Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3928466

Browse files
committed
[canvaskit] Expose parseColorString
This is handy for interacting with <input type=color> Change-Id: I7946c08ef10a2481016885d58cc52f76f5cd40e7 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272344 Reviewed-by: Kevin Lubick <kjlubick@google.com>
1 parent 77521d9 commit 3928466

File tree

5 files changed

+78
-65
lines changed

5 files changed

+78
-65
lines changed

modules/canvaskit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Added
1616
- `SkSurface.drawOnce` for drawing a single frame (in addition to already existing
1717
`SkSurface.requestAnimationFrame` for animation logic).
18+
- `CanvasKit.parseColorString` which processes color strings like "#2288FF".
1819

1920
### Changed
2021
- We now compile/ship with Emscripten v1.39.6.

modules/canvaskit/externs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
var CanvasKit = {
26-
// public API (i.e. things we declare in the pre-js file)
26+
// public API (i.e. things we declare in the pre-js file or in the cpp bindings)
2727
Color: function() {},
2828
/** @return {CanvasKit.SkRect} */
2929
LTRBRect: function() {},
@@ -73,6 +73,7 @@ var CanvasKit = {
7373
getDecodeCacheUsageBytes: function() {},
7474
getSkDataBytes: function() {},
7575
multiplyByAlpha: function() {},
76+
parseColorString: function() {},
7677
setCurrentContext: function() {},
7778
setDecodeCacheLimitBytes: function() {},
7879

modules/canvaskit/helper.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,76 @@ CanvasKit.getColorComponents = function(color) {
2929
]
3030
}
3131

32+
// parseColorString takes in a CSS color value and returns a CanvasKit.Color
33+
// (which is just a 32 bit integer, 8 bits per channel). An optional colorMap
34+
// may be provided which maps custom strings to values (e.g. {'springgreen':4278255487}).
35+
// In the CanvasKit canvas2d shim layer, we provide this map for processing
36+
// canvas2d calls, but not here for code size reasons.
37+
CanvasKit.parseColorString = function(colorStr, colorMap) {
38+
colorStr = colorStr.toLowerCase();
39+
// See https://drafts.csswg.org/css-color/#typedef-hex-color
40+
if (colorStr.startsWith('#')) {
41+
var r, g, b, a = 255;
42+
switch (colorStr.length) {
43+
case 9: // 8 hex chars #RRGGBBAA
44+
a = parseInt(colorStr.slice(7, 9), 16);
45+
case 7: // 6 hex chars #RRGGBB
46+
r = parseInt(colorStr.slice(1, 3), 16);
47+
g = parseInt(colorStr.slice(3, 5), 16);
48+
b = parseInt(colorStr.slice(5, 7), 16);
49+
break;
50+
case 5: // 4 hex chars #RGBA
51+
// multiplying by 17 is the same effect as
52+
// appending another character of the same value
53+
// e.g. e => ee == 14 => 238
54+
a = parseInt(colorStr.slice(4, 5), 16) * 17;
55+
case 4: // 6 hex chars #RGB
56+
r = parseInt(colorStr.slice(1, 2), 16) * 17;
57+
g = parseInt(colorStr.slice(2, 3), 16) * 17;
58+
b = parseInt(colorStr.slice(3, 4), 16) * 17;
59+
break;
60+
}
61+
return CanvasKit.Color(r, g, b, a/255);
62+
63+
} else if (colorStr.startsWith('rgba')) {
64+
// Trim off rgba( and the closing )
65+
colorStr = colorStr.slice(5, -1);
66+
var nums = colorStr.split(',');
67+
return CanvasKit.Color(+nums[0], +nums[1], +nums[2],
68+
valueOrPercent(nums[3]));
69+
} else if (colorStr.startsWith('rgb')) {
70+
// Trim off rgba( and the closing )
71+
colorStr = colorStr.slice(4, -1);
72+
var nums = colorStr.split(',');
73+
// rgb can take 3 or 4 arguments
74+
return CanvasKit.Color(+nums[0], +nums[1], +nums[2],
75+
valueOrPercent(nums[3]));
76+
} else if (colorStr.startsWith('gray(')) {
77+
// TODO
78+
} else if (colorStr.startsWith('hsl')) {
79+
// TODO
80+
} else if (colorMap) {
81+
// Try for named color
82+
var nc = colorMap[colorStr];
83+
if (nc !== undefined) {
84+
return nc;
85+
}
86+
}
87+
SkDebug('unrecognized color ' + colorStr);
88+
return CanvasKit.BLACK;
89+
}
90+
91+
function valueOrPercent(aStr) {
92+
if (aStr === undefined) {
93+
return 1; // default to opaque.
94+
}
95+
var a = parseFloat(aStr);
96+
if (aStr && aStr.indexOf('%') !== -1) {
97+
return a / 100;
98+
}
99+
return a;
100+
}
101+
32102
CanvasKit.multiplyByAlpha = function(color, alpha) {
33103
if (alpha === 1) {
34104
return color;

modules/canvaskit/htmlcanvas/color.js

Lines changed: 1 addition & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/canvaskit/tests/canvas2d.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('CanvasKit\'s Canvas 2d Behavior', function() {
2121

2222
it('parses hex color strings', function(done) {
2323
LoadCanvasKit.then(catchException(done, () => {
24-
const parseColor = CanvasKit._testing.parseColor;
24+
const parseColor = CanvasKit.parseColorString;
2525
expect(parseColor('#FED')).toEqual(
2626
CanvasKit.Color(hex('FF'), hex('EE'), hex('DD'), 1));
2727
expect(parseColor('#FEDC')).toEqual(
@@ -35,7 +35,7 @@ describe('CanvasKit\'s Canvas 2d Behavior', function() {
3535
});
3636
it('parses rgba color strings', function(done) {
3737
LoadCanvasKit.then(catchException(done, () => {
38-
const parseColor = CanvasKit._testing.parseColor;
38+
const parseColor = CanvasKit.parseColorString;
3939
expect(parseColor('rgba(117, 33, 64, 0.75)')).toEqual(
4040
CanvasKit.Color(117, 33, 64, 0.75));
4141
expect(parseColor('rgb(117, 33, 64, 0.75)')).toEqual(
@@ -55,6 +55,8 @@ describe('CanvasKit\'s Canvas 2d Behavior', function() {
5555
});
5656
it('parses named color strings', function(done) {
5757
LoadCanvasKit.then(catchException(done, () => {
58+
// Keep this one as the _testing version, because we don't include the large
59+
// color map by default.
5860
const parseColor = CanvasKit._testing.parseColor;
5961
expect(parseColor('grey')).toEqual(
6062
CanvasKit.Color(128, 128, 128, 1.0));

0 commit comments

Comments
 (0)