From b777cfb11e5edc32e61df2dd33909c37a7efe2e5 Mon Sep 17 00:00:00 2001 From: Paul Grant Date: Fri, 24 Apr 2020 16:48:33 -0400 Subject: [PATCH] feat: adding color/hsl and color/hsl-4 transforms (#383) Only difference between the two is hsl-4 uses the new hsl syntax (CSS color module level 4 color spec syntax). Both transforms handle colors with alpha transparency. --- __tests__/common/transforms.test.js | 32 ++++++++++++++++++++ lib/common/transforms.js | 47 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/__tests__/common/transforms.test.js b/__tests__/common/transforms.test.js index 4841b01a4..691e2ef2f 100644 --- a/__tests__/common/transforms.test.js +++ b/__tests__/common/transforms.test.js @@ -325,6 +325,38 @@ describe('common', () => { }); }); + describe('color/hsl-4', () => { + it('should handle normal colors', () => { + var value = transforms["color/hsl-4"].transformer({ + value: "#009688" + }); + expect(value).toBe("hsl(174 100% 29%)"); + }); + + it('should handle colors with transparency', () => { + var value = transforms["color/hsl-4"].transformer({ + value: "#00968899" + }); + expect(value).toBe("hsl(174 100% 29% / 0.6)"); + }); + }); + + describe('color/hsl', () => { + it('should handle normal colors', () => { + var value = transforms["color/hsl"].transformer({ + value: "#009688" + }); + expect(value).toBe("hsl(174, 100%, 29%)"); + }); + + it('should handle colors with transparency', () => { + var value = transforms["color/hsl"].transformer({ + value: "#00968899" + }); + expect(value).toBe("hsla(174, 100%, 29%, 0.6)"); + }); + }); + describe('color/UIColor', () => { it('should handle normal colors', () => { var value = transforms["color/UIColor"].transformer({ diff --git a/lib/common/transforms.js b/lib/common/transforms.js index fd1277db4..5343309d7 100644 --- a/lib/common/transforms.js +++ b/lib/common/transforms.js @@ -298,6 +298,53 @@ module.exports = { } }, + /** + * Transforms the value into an HSL string or HSLA if alpha is present. Better browser support than color/hsl-4 + * + * ```js + * // Matches: prop.attributes.category === 'color' + * // Returns: + * "hsl(174, 100%, 29%)" + * "hsl(174, 100%, 29%, .5)" + * ``` + * + * @memberof Transforms + */ + 'color/hsl': { + type: 'value', + matcher: isColor, + transformer: function (prop) { + return Color(prop.value).toHslString(); + } + }, + + /** + * Transforms the value into an HSL string, using fourth argument if alpha is present. + * + * ```js + * // Matches: prop.attributes.category === 'color' + * // Returns: + * "hsl(174 100% 29%)" + * "hsl(174 100% 29% / .5)" + * ``` + * + * @memberof Transforms + */ + 'color/hsl-4': { + type: 'value', + matcher: isColor, + transformer: function (prop) { + var color = Color(prop.value); + var o = color.toHsl() + var vals = `${Math.round(o.h)} ${Math.round(o.s * 100)}% ${Math.round(o.l * 100)}%` + if (color.getAlpha() === 1) { + return `hsl(${vals})` + } else { + return `hsl(${vals} / ${o.a})` + } + } + }, + /** * Transforms the value into an 6-digit hex string *