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

[FIX] UIColor transformer - Increase to 3 decimals to retain 8bit precision #314

Merged
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
22 changes: 18 additions & 4 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,21 @@ describe('common', () => {
var value = transforms["color/UIColor"].transformer({
value: "#aaaaaa"
});
expect(value).toBe("[UIColor colorWithRed:0.67f green:0.67f blue:0.67f alpha:1.00f]");
expect(value).toBe("[UIColor colorWithRed:0.667f green:0.667f blue:0.667f alpha:1.000f]");
});

it('should retain enough precision when converting to decimal', () => {
var value = transforms["color/UIColor"].transformer({
value: "#1d1d1d"
});
expect(value).toBe("[UIColor colorWithRed:0.114f green:0.114f blue:0.114f alpha:1.000f]");
});

it('should handle colors with transparency', () => {
var value = transforms["color/UIColor"].transformer({
value: "#aaaaaa99"
});
expect(value).toBe("[UIColor colorWithRed:0.67f green:0.67f blue:0.67f alpha:0.60f]");
expect(value).toBe("[UIColor colorWithRed:0.667f green:0.667f blue:0.667f alpha:0.600f]");
});
});

Expand All @@ -302,14 +309,21 @@ describe('common', () => {
var value = transforms["color/UIColorSwift"].transformer({
value: "#aaaaaa"
});
expect(value).toBe("UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:1)");
expect(value).toBe("UIColor(red: 0.667, green: 0.667, blue: 0.667, alpha:1)");
});

it('should retain enough precision when converting to decimal', () => {
var value = transforms["color/UIColorSwift"].transformer({
value: "#1d1d1d"
});
expect(value).toBe("UIColor(red: 0.114, green: 0.114, blue: 0.114, 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)");
expect(value).toBe("UIColor(red: 0.667, green: 0.667, blue: 0.667, alpha:0.6)");
});
});

Expand Down
18 changes: 9 additions & 9 deletions lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ module.exports = {
* ```objectivec
* // Matches: prop.attributes.category === 'color'
* // Returns:
* [UIColor colorWithRed:0.00f green:0.59f blue:0.53f alpha:1.0f]
* [UIColor colorWithRed:0.114f green:0.114f blue:0.114f alpha:1.000f]
* ```
*
* @memberof Transforms
Expand All @@ -361,10 +361,10 @@ module.exports = {
matcher: isColor,
transformer: function (prop) {
var rgb = Color(prop.value).toRgb();
return '[UIColor colorWithRed:' + (rgb.r/255).toFixed(2) + 'f' +
' green:' + (rgb.g/255).toFixed(2) + 'f' +
' blue:' + (rgb.b/255).toFixed(2) + 'f' +
' alpha:' + rgb.a.toFixed(2) + 'f]';
return '[UIColor colorWithRed:' + (rgb.r/255).toFixed(3) + 'f' +
' green:' + (rgb.g/255).toFixed(3) + 'f' +
' blue:' + (rgb.b/255).toFixed(3) + 'f' +
' alpha:' + rgb.a.toFixed(3) + 'f]';
}
},

Expand All @@ -374,7 +374,7 @@ module.exports = {
* ```swift
* // Matches: prop.attributes.category === 'color'
* // Returns:
* UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:0.6)
* UIColor(red: 0.667, green: 0.667, blue: 0.667, alpha:0.6)
* ```
*
* @memberof Transforms
Expand All @@ -384,9 +384,9 @@ module.exports = {
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);
const rFixed = (r / 255.0).toFixed(3);
const gFixed = (g / 255.0).toFixed(3);
const bFixed = (b / 255.0).toFixed(3);
return `UIColor(red: ${rFixed}, green: ${gFixed}, blue: ${bFixed}, alpha:${a})`;
}
},
Expand Down