Skip to content

Commit

Permalink
feat(transforms): Add UIColor transform for swift (#250)
Browse files Browse the repository at this point in the history
Added UIColor transform for swift based on apple's spec:
https://developer.apple.com/documentation/uikit/uicolor/1621925-init

re #161
  • Loading branch information
chazzmoney authored and dbanksdesign committed Feb 19, 2019
1 parent 09fc43f commit a62d880
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
},
"env": {
"node": true,
"mocha": true
"mocha": true,
"jest": true
},
"globals": {
"Buffer": true,
Expand Down
16 changes: 16 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
23 changes: 23 additions & 0 deletions lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit a62d880

Please sign in to comment.