diff --git a/.eslintrc.json b/.eslintrc.json index 25ab723db..a7dff9f94 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,8 @@ }, "env": { "node": true, - "mocha": true + "mocha": true, + "jest": true }, "globals": { "Buffer": true, diff --git a/__tests__/common/transforms.test.js b/__tests__/common/transforms.test.js index bcb8aff1a..af5884305 100644 --- a/__tests__/common/transforms.test.js +++ b/__tests__/common/transforms.test.js @@ -277,6 +277,22 @@ describe('common', () => { }); }); + describe('color/UIColorSwift', () => { + it('should handle normal colors', () => { + var value = transforms["color/UIColorSwift"].transformer({ + value: "#aaaaaa" + }); + expect(value).toBe("UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:1)"); + }); + + it('should handle colors with transparency', () => { + var value = transforms["color/UIColorSwift"].transformer({ + value: "#aaaaaa99" + }); + expect(value).toBe("UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:0.6)"); + }); + }); + describe('color/css', () => { it('should handle normal colors', () => { diff --git a/lib/common/transforms.js b/lib/common/transforms.js index 542a5ec9e..38a9e1d15 100644 --- a/lib/common/transforms.js +++ b/lib/common/transforms.js @@ -336,6 +336,29 @@ module.exports = { } }, + /** + * Transforms the value into an UIColor swift class for iOS + * + * ```swift + * // Matches: prop.attributes.category === 'color' + * // Returns: + * UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:0.6) + * ``` + * + * @memberof Transforms + */ + 'color/UIColorSwift': { + type: 'value', + matcher: isColor, + transformer: function (prop) { + const { r, g, b, a } = Color(prop.value).toRgb(); + const rFixed = (r / 255.0).toFixed(2); + const gFixed = (g / 255.0).toFixed(2); + const bFixed = (b / 255.0).toFixed(2); + return `UIColor(red: ${rFixed}, green: ${gFixed}, blue: ${bFixed}, alpha:${a})`; + } + }, + /** * Transforms the value into a hex or rgb string depending on if it has transparency *