• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Functions dealing with parsing/stringifying color go here.
2
3// Create the following with
4// node ./htmlcanvas/_namedcolors.js --expose-wasm
5// JS/closure doesn't have a constexpr like thing which
6// would really help here. Since we don't, we pre-compute
7// the map, which saves (a tiny amount of) startup time
8// and (a small amount of) code size.
9/* @dict */
10var colorMap = {
11  'aliceblue':            Float32Array.of(0.941, 0.973, 1.000, 1.000),
12  'antiquewhite':         Float32Array.of(0.980, 0.922, 0.843, 1.000),
13  'aqua':                 Float32Array.of(0.000, 1.000, 1.000, 1.000),
14  'aquamarine':           Float32Array.of(0.498, 1.000, 0.831, 1.000),
15  'azure':                Float32Array.of(0.941, 1.000, 1.000, 1.000),
16  'beige':                Float32Array.of(0.961, 0.961, 0.863, 1.000),
17  'bisque':               Float32Array.of(1.000, 0.894, 0.769, 1.000),
18  'black':                Float32Array.of(0.000, 0.000, 0.000, 1.000),
19  'blanchedalmond':       Float32Array.of(1.000, 0.922, 0.804, 1.000),
20  'blue':                 Float32Array.of(0.000, 0.000, 1.000, 1.000),
21  'blueviolet':           Float32Array.of(0.541, 0.169, 0.886, 1.000),
22  'brown':                Float32Array.of(0.647, 0.165, 0.165, 1.000),
23  'burlywood':            Float32Array.of(0.871, 0.722, 0.529, 1.000),
24  'cadetblue':            Float32Array.of(0.373, 0.620, 0.627, 1.000),
25  'chartreuse':           Float32Array.of(0.498, 1.000, 0.000, 1.000),
26  'chocolate':            Float32Array.of(0.824, 0.412, 0.118, 1.000),
27  'coral':                Float32Array.of(1.000, 0.498, 0.314, 1.000),
28  'cornflowerblue':       Float32Array.of(0.392, 0.584, 0.929, 1.000),
29  'cornsilk':             Float32Array.of(1.000, 0.973, 0.863, 1.000),
30  'crimson':              Float32Array.of(0.863, 0.078, 0.235, 1.000),
31  'cyan':                 Float32Array.of(0.000, 1.000, 1.000, 1.000),
32  'darkblue':             Float32Array.of(0.000, 0.000, 0.545, 1.000),
33  'darkcyan':             Float32Array.of(0.000, 0.545, 0.545, 1.000),
34  'darkgoldenrod':        Float32Array.of(0.722, 0.525, 0.043, 1.000),
35  'darkgray':             Float32Array.of(0.663, 0.663, 0.663, 1.000),
36  'darkgreen':            Float32Array.of(0.000, 0.392, 0.000, 1.000),
37  'darkgrey':             Float32Array.of(0.663, 0.663, 0.663, 1.000),
38  'darkkhaki':            Float32Array.of(0.741, 0.718, 0.420, 1.000),
39  'darkmagenta':          Float32Array.of(0.545, 0.000, 0.545, 1.000),
40  'darkolivegreen':       Float32Array.of(0.333, 0.420, 0.184, 1.000),
41  'darkorange':           Float32Array.of(1.000, 0.549, 0.000, 1.000),
42  'darkorchid':           Float32Array.of(0.600, 0.196, 0.800, 1.000),
43  'darkred':              Float32Array.of(0.545, 0.000, 0.000, 1.000),
44  'darksalmon':           Float32Array.of(0.914, 0.588, 0.478, 1.000),
45  'darkseagreen':         Float32Array.of(0.561, 0.737, 0.561, 1.000),
46  'darkslateblue':        Float32Array.of(0.282, 0.239, 0.545, 1.000),
47  'darkslategray':        Float32Array.of(0.184, 0.310, 0.310, 1.000),
48  'darkslategrey':        Float32Array.of(0.184, 0.310, 0.310, 1.000),
49  'darkturquoise':        Float32Array.of(0.000, 0.808, 0.820, 1.000),
50  'darkviolet':           Float32Array.of(0.580, 0.000, 0.827, 1.000),
51  'deeppink':             Float32Array.of(1.000, 0.078, 0.576, 1.000),
52  'deepskyblue':          Float32Array.of(0.000, 0.749, 1.000, 1.000),
53  'dimgray':              Float32Array.of(0.412, 0.412, 0.412, 1.000),
54  'dimgrey':              Float32Array.of(0.412, 0.412, 0.412, 1.000),
55  'dodgerblue':           Float32Array.of(0.118, 0.565, 1.000, 1.000),
56  'firebrick':            Float32Array.of(0.698, 0.133, 0.133, 1.000),
57  'floralwhite':          Float32Array.of(1.000, 0.980, 0.941, 1.000),
58  'forestgreen':          Float32Array.of(0.133, 0.545, 0.133, 1.000),
59  'fuchsia':              Float32Array.of(1.000, 0.000, 1.000, 1.000),
60  'gainsboro':            Float32Array.of(0.863, 0.863, 0.863, 1.000),
61  'ghostwhite':           Float32Array.of(0.973, 0.973, 1.000, 1.000),
62  'gold':                 Float32Array.of(1.000, 0.843, 0.000, 1.000),
63  'goldenrod':            Float32Array.of(0.855, 0.647, 0.125, 1.000),
64  'gray':                 Float32Array.of(0.502, 0.502, 0.502, 1.000),
65  'green':                Float32Array.of(0.000, 0.502, 0.000, 1.000),
66  'greenyellow':          Float32Array.of(0.678, 1.000, 0.184, 1.000),
67  'grey':                 Float32Array.of(0.502, 0.502, 0.502, 1.000),
68  'honeydew':             Float32Array.of(0.941, 1.000, 0.941, 1.000),
69  'hotpink':              Float32Array.of(1.000, 0.412, 0.706, 1.000),
70  'indianred':            Float32Array.of(0.804, 0.361, 0.361, 1.000),
71  'indigo':               Float32Array.of(0.294, 0.000, 0.510, 1.000),
72  'ivory':                Float32Array.of(1.000, 1.000, 0.941, 1.000),
73  'khaki':                Float32Array.of(0.941, 0.902, 0.549, 1.000),
74  'lavender':             Float32Array.of(0.902, 0.902, 0.980, 1.000),
75  'lavenderblush':        Float32Array.of(1.000, 0.941, 0.961, 1.000),
76  'lawngreen':            Float32Array.of(0.486, 0.988, 0.000, 1.000),
77  'lemonchiffon':         Float32Array.of(1.000, 0.980, 0.804, 1.000),
78  'lightblue':            Float32Array.of(0.678, 0.847, 0.902, 1.000),
79  'lightcoral':           Float32Array.of(0.941, 0.502, 0.502, 1.000),
80  'lightcyan':            Float32Array.of(0.878, 1.000, 1.000, 1.000),
81  'lightgoldenrodyellow': Float32Array.of(0.980, 0.980, 0.824, 1.000),
82  'lightgray':            Float32Array.of(0.827, 0.827, 0.827, 1.000),
83  'lightgreen':           Float32Array.of(0.565, 0.933, 0.565, 1.000),
84  'lightgrey':            Float32Array.of(0.827, 0.827, 0.827, 1.000),
85  'lightpink':            Float32Array.of(1.000, 0.714, 0.757, 1.000),
86  'lightsalmon':          Float32Array.of(1.000, 0.627, 0.478, 1.000),
87  'lightseagreen':        Float32Array.of(0.125, 0.698, 0.667, 1.000),
88  'lightskyblue':         Float32Array.of(0.529, 0.808, 0.980, 1.000),
89  'lightslategray':       Float32Array.of(0.467, 0.533, 0.600, 1.000),
90  'lightslategrey':       Float32Array.of(0.467, 0.533, 0.600, 1.000),
91  'lightsteelblue':       Float32Array.of(0.690, 0.769, 0.871, 1.000),
92  'lightyellow':          Float32Array.of(1.000, 1.000, 0.878, 1.000),
93  'lime':                 Float32Array.of(0.000, 1.000, 0.000, 1.000),
94  'limegreen':            Float32Array.of(0.196, 0.804, 0.196, 1.000),
95  'linen':                Float32Array.of(0.980, 0.941, 0.902, 1.000),
96  'magenta':              Float32Array.of(1.000, 0.000, 1.000, 1.000),
97  'maroon':               Float32Array.of(0.502, 0.000, 0.000, 1.000),
98  'mediumaquamarine':     Float32Array.of(0.400, 0.804, 0.667, 1.000),
99  'mediumblue':           Float32Array.of(0.000, 0.000, 0.804, 1.000),
100  'mediumorchid':         Float32Array.of(0.729, 0.333, 0.827, 1.000),
101  'mediumpurple':         Float32Array.of(0.576, 0.439, 0.859, 1.000),
102  'mediumseagreen':       Float32Array.of(0.235, 0.702, 0.443, 1.000),
103  'mediumslateblue':      Float32Array.of(0.482, 0.408, 0.933, 1.000),
104  'mediumspringgreen':    Float32Array.of(0.000, 0.980, 0.604, 1.000),
105  'mediumturquoise':      Float32Array.of(0.282, 0.820, 0.800, 1.000),
106  'mediumvioletred':      Float32Array.of(0.780, 0.082, 0.522, 1.000),
107  'midnightblue':         Float32Array.of(0.098, 0.098, 0.439, 1.000),
108  'mintcream':            Float32Array.of(0.961, 1.000, 0.980, 1.000),
109  'mistyrose':            Float32Array.of(1.000, 0.894, 0.882, 1.000),
110  'moccasin':             Float32Array.of(1.000, 0.894, 0.710, 1.000),
111  'navajowhite':          Float32Array.of(1.000, 0.871, 0.678, 1.000),
112  'navy':                 Float32Array.of(0.000, 0.000, 0.502, 1.000),
113  'oldlace':              Float32Array.of(0.992, 0.961, 0.902, 1.000),
114  'olive':                Float32Array.of(0.502, 0.502, 0.000, 1.000),
115  'olivedrab':            Float32Array.of(0.420, 0.557, 0.137, 1.000),
116  'orange':               Float32Array.of(1.000, 0.647, 0.000, 1.000),
117  'orangered':            Float32Array.of(1.000, 0.271, 0.000, 1.000),
118  'orchid':               Float32Array.of(0.855, 0.439, 0.839, 1.000),
119  'palegoldenrod':        Float32Array.of(0.933, 0.910, 0.667, 1.000),
120  'palegreen':            Float32Array.of(0.596, 0.984, 0.596, 1.000),
121  'paleturquoise':        Float32Array.of(0.686, 0.933, 0.933, 1.000),
122  'palevioletred':        Float32Array.of(0.859, 0.439, 0.576, 1.000),
123  'papayawhip':           Float32Array.of(1.000, 0.937, 0.835, 1.000),
124  'peachpuff':            Float32Array.of(1.000, 0.855, 0.725, 1.000),
125  'peru':                 Float32Array.of(0.804, 0.522, 0.247, 1.000),
126  'pink':                 Float32Array.of(1.000, 0.753, 0.796, 1.000),
127  'plum':                 Float32Array.of(0.867, 0.627, 0.867, 1.000),
128  'powderblue':           Float32Array.of(0.690, 0.878, 0.902, 1.000),
129  'purple':               Float32Array.of(0.502, 0.000, 0.502, 1.000),
130  'rebeccapurple':        Float32Array.of(0.400, 0.200, 0.600, 1.000),
131  'red':                  Float32Array.of(1.000, 0.000, 0.000, 1.000),
132  'rosybrown':            Float32Array.of(0.737, 0.561, 0.561, 1.000),
133  'royalblue':            Float32Array.of(0.255, 0.412, 0.882, 1.000),
134  'saddlebrown':          Float32Array.of(0.545, 0.271, 0.075, 1.000),
135  'salmon':               Float32Array.of(0.980, 0.502, 0.447, 1.000),
136  'sandybrown':           Float32Array.of(0.957, 0.643, 0.376, 1.000),
137  'seagreen':             Float32Array.of(0.180, 0.545, 0.341, 1.000),
138  'seashell':             Float32Array.of(1.000, 0.961, 0.933, 1.000),
139  'sienna':               Float32Array.of(0.627, 0.322, 0.176, 1.000),
140  'silver':               Float32Array.of(0.753, 0.753, 0.753, 1.000),
141  'skyblue':              Float32Array.of(0.529, 0.808, 0.922, 1.000),
142  'slateblue':            Float32Array.of(0.416, 0.353, 0.804, 1.000),
143  'slategray':            Float32Array.of(0.439, 0.502, 0.565, 1.000),
144  'slategrey':            Float32Array.of(0.439, 0.502, 0.565, 1.000),
145  'snow':                 Float32Array.of(1.000, 0.980, 0.980, 1.000),
146  'springgreen':          Float32Array.of(0.000, 1.000, 0.498, 1.000),
147  'steelblue':            Float32Array.of(0.275, 0.510, 0.706, 1.000),
148  'tan':                  Float32Array.of(0.824, 0.706, 0.549, 1.000),
149  'teal':                 Float32Array.of(0.000, 0.502, 0.502, 1.000),
150  'thistle':              Float32Array.of(0.847, 0.749, 0.847, 1.000),
151  'tomato':               Float32Array.of(1.000, 0.388, 0.278, 1.000),
152  'transparent':          Float32Array.of(0.000, 0.000, 0.000, 0.000),
153  'turquoise':            Float32Array.of(0.251, 0.878, 0.816, 1.000),
154  'violet':               Float32Array.of(0.933, 0.510, 0.933, 1.000),
155  'wheat':                Float32Array.of(0.961, 0.871, 0.702, 1.000),
156  'white':                Float32Array.of(1.000, 1.000, 1.000, 1.000),
157  'whitesmoke':           Float32Array.of(0.961, 0.961, 0.961, 1.000),
158  'yellow':               Float32Array.of(1.000, 1.000, 0.000, 1.000),
159  'yellowgreen':          Float32Array.of(0.604, 0.804, 0.196, 1.000),
160}
161
162function colorToString(skcolor) {
163  // https://html.spec.whatwg.org/multipage/canvas.html#serialisation-of-a-color
164  var components = CanvasKit.getColorComponents(skcolor);
165  var r = components[0];
166  var g = components[1];
167  var b = components[2];
168  var a = components[3];
169  if (a === 1.0) {
170    // hex
171    r = r.toString(16).toLowerCase();
172    g = g.toString(16).toLowerCase();
173    b = b.toString(16).toLowerCase();
174    r = (r.length === 1 ? '0'+r: r);
175    g = (g.length === 1 ? '0'+g: g);
176    b = (b.length === 1 ? '0'+b: b);
177    return '#'+r+g+b;
178  } else {
179    a = (a === 0 || a === 1) ? a : a.toFixed(8);
180    return 'rgba('+r+', '+g+', '+b+', '+a+')';
181  }
182}
183
184function parseColor(colorStr) {
185  return CanvasKit.parseColorString(colorStr, colorMap);
186}
187
188CanvasKit._testing['parseColor'] = parseColor;
189CanvasKit._testing['colorToString'] = colorToString;
190