Skip to content

Commit 1d4389a

Browse files
Fix colorHex to handle alpha values (#1441)
Co-authored-by: jorenbroekema <[email protected]>
1 parent 177c25e commit 1d4389a

File tree

6 files changed

+132
-12
lines changed

6 files changed

+132
-12
lines changed

.changeset/few-dragons-sleep.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
- `'color/hex'` (`colorHex` enum) built-in transform can now handle alpha channels properly by outputting hex8 format if needed. This also affects the transformGroups `less` and `js` which apply this transform.
6+
- `'color/hex8'` (`colorHex8` enum) built-in transform is now deprecated, use `'color/hex'` (`colorHex` enum) instead.

__tests__/common/transforms.test.js

+104-5
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,15 @@ describe('common', () => {
271271
{},
272272
{},
273273
);
274-
expect(value).to.equal('#aaaaaa');
274+
const value2 = transforms[colorHex].transform(
275+
{
276+
value: '#aaaaaaff',
277+
},
278+
{},
279+
{},
280+
);
281+
expect(value).to.equal('#aaaaaaaa');
282+
expect(value2).to.equal('#aaaaaa');
275283
});
276284

277285
it('should handle rgb colors', () => {
@@ -285,6 +293,25 @@ describe('common', () => {
285293
expect(value).to.equal('#aaaaaa');
286294
});
287295

296+
it('should handle rgba colors', () => {
297+
const value = transforms[colorHex].transform(
298+
{
299+
value: 'rgba(170,170,170,0.5)',
300+
},
301+
{},
302+
{},
303+
);
304+
const value2 = transforms[colorHex].transform(
305+
{
306+
value: 'rgba(170,170,170,1.0)',
307+
},
308+
{},
309+
{},
310+
);
311+
expect(value).to.equal('#aaaaaa80');
312+
expect(value2).to.equal('#aaaaaa');
313+
});
314+
288315
it('should handle rgb (object) colors', () => {
289316
const value = transforms[colorHex].transform(
290317
{
@@ -297,18 +324,69 @@ describe('common', () => {
297324
{},
298325
{},
299326
);
327+
expect(value).to.equal('#aaaaaa');
328+
});
329+
330+
it('should handle rgba (object) colors', () => {
331+
const value = transforms[colorHex].transform(
332+
{
333+
value: {
334+
r: '170',
335+
g: '170',
336+
b: '170',
337+
a: '0.5',
338+
},
339+
},
340+
{},
341+
{},
342+
);
300343
const value2 = transforms[colorHex].transform(
301344
{
302-
value: 'rgb(170,170,170)',
345+
value: {
346+
r: '170',
347+
g: '170',
348+
b: '170',
349+
a: '1.0',
350+
},
303351
},
304352
{},
305353
{},
306354
);
307-
expect(value).to.equal('#aaaaaa');
355+
expect(value).to.equal('#aaaaaa80');
308356
expect(value2).to.equal('#aaaaaa');
309357
});
310358

311359
it('should handle hsl colors', () => {
360+
const value = transforms[colorHex].transform(
361+
{
362+
value: 'hsl(0,0,0.5)',
363+
},
364+
{},
365+
{},
366+
);
367+
expect(value).to.equal('#808080');
368+
});
369+
370+
it('should handle hsla colors', () => {
371+
const value = transforms[colorHex].transform(
372+
{
373+
value: 'hsla(0,0,0.5,0.5)',
374+
},
375+
{},
376+
{},
377+
);
378+
const value2 = transforms[colorHex].transform(
379+
{
380+
value: 'hsla(0,0,0.5,1.0)',
381+
},
382+
{},
383+
{},
384+
);
385+
expect(value).to.equal('#80808080');
386+
expect(value2).to.equal('#808080');
387+
});
388+
389+
it('should handle hsl (object) colors', () => {
312390
const value = transforms[colorHex].transform(
313391
{
314392
value: {
@@ -320,14 +398,35 @@ describe('common', () => {
320398
{},
321399
{},
322400
);
401+
expect(value).to.equal('#808080');
402+
});
403+
404+
it('should handle hsla (object) colors', () => {
405+
const value = transforms[colorHex].transform(
406+
{
407+
value: {
408+
h: '0',
409+
s: '0',
410+
l: '0.5',
411+
a: '0.5',
412+
},
413+
},
414+
{},
415+
{},
416+
);
323417
const value2 = transforms[colorHex].transform(
324418
{
325-
value: 'hsl(0,0,0.5)',
419+
value: {
420+
h: '0',
421+
s: '0',
422+
l: '0.5',
423+
a: '1.0',
424+
},
326425
},
327426
{},
328427
{},
329428
);
330-
expect(value).to.equal('#808080');
429+
expect(value).to.equal('#80808080');
331430
expect(value2).to.equal('#808080');
332431
});
333432
});

docs/src/content/docs/reference/Hooks/Transforms/predefined.md renamed to docs/src/content/docs/reference/Hooks/Transforms/predefined.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: Built-in transforms
33
---
44

5+
import { Badge } from '@astrojs/starlight/components';
6+
57
:::tip
68
You can find the source code of the built-in transforms here:
79
[lib/common/transforms.js](https://github.com/amzn/style-dictionary/blob/main/lib/common/transforms.js)
@@ -179,7 +181,9 @@ Transforms the value into an 6-digit hex string
179181

180182
---
181183

182-
### color/hex8
184+
### color/hex8 <Badge text="Deprecated" variant="danger" />
185+
186+
> Use color/hex instead, which also supports hex8.
183187
184188
Transforms the value into an 8-digit hex string
185189

docs/src/content/docs/reference/enums.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ export const transforms = {
238238
colorHsl: 'color/hsl',
239239
colorHsl4: 'color/hsl-4',
240240
colorHex: 'color/hex',
241-
colorHex8: 'color/hex8',
242241
colorHex8android: 'color/hex8android',
243242
colorComposeColor: 'color/composeColor',
244243
colorUIColor: 'color/UIColor',
@@ -281,6 +280,10 @@ export const transforms = {
281280
contentFlutterLiteral: 'content/flutter/literal',
282281
assetFlutterLiteral: 'asset/flutter/literal',
283282
sizeFlutterRemToDouble: 'size/flutter/remToDouble',
283+
/**
284+
* @deprecated use 'color/hex instead
285+
*/
286+
colorHex8: 'color/hex8',
284287
};
285288
```
286289

lib/common/transforms.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,12 @@ export default {
460460
},
461461

462462
/**
463-
* Transforms the value into an 6-digit hex string
463+
* Transforms the value into a 6-digit (if alpha is undefined or 1.0) or 8-digit hex string
464464
*
465465
* ```js
466466
* // Matches: token.type === 'color'
467467
* // Returns:
468-
* "#009688"
468+
* "#009688DE"
469469
* ```
470470
*
471471
* @memberof Transforms
@@ -474,7 +474,12 @@ export default {
474474
type: value,
475475
filter: isColor,
476476
transform: function (token, _, options) {
477-
return Color(options.usesDtcg ? token.$value : token.value).toHexString();
477+
const color = Color(options.usesDtcg ? token.$value : token.value);
478+
if (color.getAlpha() === 1) {
479+
return color.toHexString();
480+
} else {
481+
return color.toHex8String();
482+
}
478483
},
479484
},
480485

@@ -486,7 +491,7 @@ export default {
486491
* // Returns:
487492
* "#009688ff"
488493
* ```
489-
*
494+
* @deprecated use colorHex ('color/hex') instead, which supports hex8 now.
490495
* @memberof Transforms
491496
*/
492497
[transforms.colorHex8]: {

lib/enums/transforms.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const transforms = {
1111
colorHsl: /** @type {'color/hsl'} */ ('color/hsl'),
1212
colorHsl4: /** @type {'color/hsl-4'} */ ('color/hsl-4'),
1313
colorHex: /** @type {'color/hex'} */ ('color/hex'),
14-
colorHex8: /** @type {'color/hex8'} */ ('color/hex8'),
1514
colorHex8android: /** @type {'color/hex8android'} */ ('color/hex8android'),
1615
colorComposeColor: /** @type {'color/composeColor'} */ ('color/composeColor'),
1716
colorUIColor: /** @type {'color/UIColor'} */ ('color/UIColor'),
@@ -54,4 +53,8 @@ export const transforms = {
5453
contentFlutterLiteral: /** @type {'content/flutter/literal'} */ ('content/flutter/literal'),
5554
assetFlutterLiteral: /** @type {'asset/flutter/literal'} */ ('asset/flutter/literal'),
5655
sizeFlutterRemToDouble: /** @type {'size/flutter/remToDouble'} */ ('size/flutter/remToDouble'),
56+
/**
57+
* @deprecated use 'color/hex instead
58+
*/
59+
colorHex8: /** @type {'color/hex8'} */ ('color/hex8'),
5760
};

0 commit comments

Comments
 (0)