Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@davixyz addition of swiftColor #250

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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