Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: Source/core/platform/graphics/Color.cpp

Issue 42313002: Move Color.*, DashArray and DrawLooper to Source/platform/ (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Add missing moved files Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/platform/graphics/Color.h ('k') | Source/core/platform/graphics/DashArray.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "core/platform/graphics/Color.h"
28
29 #include "core/platform/HashTools.h"
30 #include "wtf/Assertions.h"
31 #include "wtf/DecimalNumber.h"
32 #include "wtf/HexNumber.h"
33 #include "wtf/MathExtras.h"
34 #include "wtf/text/StringBuilder.h"
35
36 using namespace std;
37
38 namespace WebCore {
39
40 #if !COMPILER(MSVC)
41 const RGBA32 Color::black;
42 const RGBA32 Color::white;
43 const RGBA32 Color::darkGray;
44 const RGBA32 Color::gray;
45 const RGBA32 Color::lightGray;
46 const RGBA32 Color::transparent;
47 #endif
48
49 static const RGBA32 lightenedBlack = 0xFF545454;
50 static const RGBA32 darkenedWhite = 0xFFABABAB;
51
52 RGBA32 makeRGB(int r, int g, int b)
53 {
54 return 0xFF000000 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | m ax(0, min(b, 255));
55 }
56
57 RGBA32 makeRGBA(int r, int g, int b, int a)
58 {
59 return max(0, min(a, 255)) << 24 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
60 }
61
62 static int colorFloatToRGBAByte(float f)
63 {
64 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's roundi ng
65 return max(0, min(static_cast<int>(lroundf(255.0f * f)), 255));
66 }
67
68 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a)
69 {
70 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | color FloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b);
71 }
72
73 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha)
74 {
75 RGBA32 rgbOnly = color & 0x00FFFFFF;
76 RGBA32 rgba = rgbOnly | colorFloatToRGBAByte(overrideAlpha) << 24;
77 return rgba;
78 }
79
80 static double calcHue(double temp1, double temp2, double hueVal)
81 {
82 if (hueVal < 0.0)
83 hueVal++;
84 else if (hueVal > 1.0)
85 hueVal--;
86 if (hueVal * 6.0 < 1.0)
87 return temp1 + (temp2 - temp1) * hueVal * 6.0;
88 if (hueVal * 2.0 < 1.0)
89 return temp2;
90 if (hueVal * 3.0 < 2.0)
91 return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0;
92 return temp1;
93 }
94
95 // Explanation of this algorithm can be found in the CSS3 Color Module
96 // specification at http://www.w3.org/TR/css3-color/#hsl-color with further
97 // explanation available at http://en.wikipedia.org/wiki/HSL_color_space
98
99 // all values are in the range of 0 to 1.0
100 RGBA32 makeRGBAFromHSLA(double hue, double saturation, double lightness, double alpha)
101 {
102 const double scaleFactor = nextafter(256.0, 0.0);
103
104 if (!saturation) {
105 int greyValue = static_cast<int>(lightness * scaleFactor);
106 return makeRGBA(greyValue, greyValue, greyValue, static_cast<int>(alpha * scaleFactor));
107 }
108
109 double temp2 = lightness < 0.5 ? lightness * (1.0 + saturation) : lightness + saturation - lightness * saturation;
110 double temp1 = 2.0 * lightness - temp2;
111
112 return makeRGBA(static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * sc aleFactor),
113 static_cast<int>(calcHue(temp1, temp2, hue) * scaleFactor),
114 static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * sc aleFactor),
115 static_cast<int>(alpha * scaleFactor));
116 }
117
118 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)
119 {
120 double colors = 1 - k;
121 int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));
122 int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));
123 int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));
124 return makeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));
125 }
126
127 // originally moved here from the CSS parser
128 template <typename CharacterType>
129 static inline bool parseHexColorInternal(const CharacterType* name, unsigned len gth, RGBA32& rgb)
130 {
131 if (length != 3 && length != 6)
132 return false;
133 unsigned value = 0;
134 for (unsigned i = 0; i < length; ++i) {
135 if (!isASCIIHexDigit(name[i]))
136 return false;
137 value <<= 4;
138 value |= toASCIIHexValue(name[i]);
139 }
140 if (length == 6) {
141 rgb = 0xFF000000 | value;
142 return true;
143 }
144 // #abc converts to #aabbcc
145 rgb = 0xFF000000
146 | (value & 0xF00) << 12 | (value & 0xF00) << 8
147 | (value & 0xF0) << 8 | (value & 0xF0) << 4
148 | (value & 0xF) << 4 | (value & 0xF);
149 return true;
150 }
151
152 bool Color::parseHexColor(const LChar* name, unsigned length, RGBA32& rgb)
153 {
154 return parseHexColorInternal(name, length, rgb);
155 }
156
157 bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb)
158 {
159 return parseHexColorInternal(name, length, rgb);
160 }
161
162 bool Color::parseHexColor(const String& name, RGBA32& rgb)
163 {
164 unsigned length = name.length();
165
166 if (!length)
167 return false;
168 if (name.is8Bit())
169 return parseHexColor(name.characters8(), name.length(), rgb);
170 return parseHexColor(name.characters16(), name.length(), rgb);
171 }
172
173 int differenceSquared(const Color& c1, const Color& c2)
174 {
175 int dR = c1.red() - c2.red();
176 int dG = c1.green() - c2.green();
177 int dB = c1.blue() - c2.blue();
178 return dR * dR + dG * dG + dB * dB;
179 }
180
181 Color::Color(const String& name)
182 {
183 if (name[0] == '#') {
184 if (name.is8Bit())
185 m_valid = parseHexColor(name.characters8() + 1, name.length() - 1, m _color);
186 else
187 m_valid = parseHexColor(name.characters16() + 1, name.length() - 1, m_color);
188 } else {
189 setNamedColor(name);
190 }
191 }
192
193 Color::Color(const char* name)
194 {
195 if (name[0] == '#') {
196 m_valid = parseHexColor(&name[1], m_color);
197 } else {
198 const NamedColor* foundColor = findColor(name, strlen(name));
199 m_color = foundColor ? foundColor->ARGBValue : 0;
200 m_valid = foundColor;
201 }
202 }
203
204 String Color::serialized() const
205 {
206 if (!hasAlpha()) {
207 StringBuilder builder;
208 builder.reserveCapacity(7);
209 builder.append('#');
210 appendByteAsHex(red(), builder, Lowercase);
211 appendByteAsHex(green(), builder, Lowercase);
212 appendByteAsHex(blue(), builder, Lowercase);
213 return builder.toString();
214 }
215
216 StringBuilder result;
217 result.reserveCapacity(28);
218 const char commaSpace[] = ", ";
219 const char rgbaParen[] = "rgba(";
220
221 result.append(rgbaParen, 5);
222 result.appendNumber(red());
223 result.append(commaSpace, 2);
224 result.appendNumber(green());
225 result.append(commaSpace, 2);
226 result.appendNumber(blue());
227 result.append(commaSpace, 2);
228
229 if (!alpha())
230 result.append('0');
231 else {
232 NumberToLStringBuffer buffer;
233 unsigned length = DecimalNumber(alpha() / 255.0).toStringDecimal(buffer, WTF::NumberToStringBufferLength);
234 result.append(buffer, length);
235 }
236
237 result.append(')');
238 return result.toString();
239 }
240
241 String Color::nameForRenderTreeAsText() const
242 {
243 if (alpha() < 0xFF)
244 return String::format("#%02X%02X%02X%02X", red(), green(), blue(), alpha ());
245 return String::format("#%02X%02X%02X", red(), green(), blue());
246 }
247
248 static inline const NamedColor* findNamedColor(const String& name)
249 {
250 char buffer[64]; // easily big enough for the longest color name
251 unsigned length = name.length();
252 if (length > sizeof(buffer) - 1)
253 return 0;
254 for (unsigned i = 0; i < length; ++i) {
255 UChar c = name[i];
256 if (!c || c > 0x7F)
257 return 0;
258 buffer[i] = toASCIILower(static_cast<char>(c));
259 }
260 buffer[length] = '\0';
261 return findColor(buffer, length);
262 }
263
264 void Color::setNamedColor(const String& name)
265 {
266 const NamedColor* foundColor = findNamedColor(name);
267 m_color = foundColor ? foundColor->ARGBValue : 0;
268 m_valid = foundColor;
269 }
270
271 Color Color::light() const
272 {
273 // Hardcode this common case for speed.
274 if (m_color == black)
275 return lightenedBlack;
276
277 const float scaleFactor = nextafterf(256.0f, 0.0f);
278
279 float r, g, b, a;
280 getRGBA(r, g, b, a);
281
282 float v = max(r, max(g, b));
283
284 if (v == 0.0f)
285 // Lightened black with alpha.
286 return Color(0x54, 0x54, 0x54, alpha());
287
288 float multiplier = min(1.0f, v + 0.33f) / v;
289
290 return Color(static_cast<int>(multiplier * r * scaleFactor),
291 static_cast<int>(multiplier * g * scaleFactor),
292 static_cast<int>(multiplier * b * scaleFactor),
293 alpha());
294 }
295
296 Color Color::dark() const
297 {
298 // Hardcode this common case for speed.
299 if (m_color == white)
300 return darkenedWhite;
301
302 const float scaleFactor = nextafterf(256.0f, 0.0f);
303
304 float r, g, b, a;
305 getRGBA(r, g, b, a);
306
307 float v = max(r, max(g, b));
308 float multiplier = max(0.0f, (v - 0.33f) / v);
309
310 return Color(static_cast<int>(multiplier * r * scaleFactor),
311 static_cast<int>(multiplier * g * scaleFactor),
312 static_cast<int>(multiplier * b * scaleFactor),
313 alpha());
314 }
315
316 static int blendComponent(int c, int a)
317 {
318 // We use white.
319 float alpha = a / 255.0f;
320 int whiteBlend = 255 - a;
321 c -= whiteBlend;
322 return static_cast<int>(c / alpha);
323 }
324
325 const int cStartAlpha = 153; // 60%
326 const int cEndAlpha = 204; // 80%;
327 const int cAlphaIncrement = 17; // Increments in between.
328
329 Color Color::blend(const Color& source) const
330 {
331 if (!alpha() || !source.hasAlpha())
332 return source;
333
334 if (!source.alpha())
335 return *this;
336
337 int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
338 int a = d / 255;
339 int r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * s ource.red()) / d;
340 int g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
341 int b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
342 return Color(r, g, b, a);
343 }
344
345 Color Color::blendWithWhite() const
346 {
347 // If the color contains alpha already, we leave it alone.
348 if (hasAlpha())
349 return *this;
350
351 Color newColor;
352 for (int alpha = cStartAlpha; alpha <= cEndAlpha; alpha += cAlphaIncrement) {
353 // We have a solid color. Convert to an equivalent color that looks the same when blended with white
354 // at the current alpha. Try using less transparency if the numbers end up being negative.
355 int r = blendComponent(red(), alpha);
356 int g = blendComponent(green(), alpha);
357 int b = blendComponent(blue(), alpha);
358
359 newColor = Color(r, g, b, alpha);
360
361 if (r >= 0 && g >= 0 && b >= 0)
362 break;
363 }
364 return newColor;
365 }
366
367 void Color::getRGBA(float& r, float& g, float& b, float& a) const
368 {
369 r = red() / 255.0f;
370 g = green() / 255.0f;
371 b = blue() / 255.0f;
372 a = alpha() / 255.0f;
373 }
374
375 void Color::getRGBA(double& r, double& g, double& b, double& a) const
376 {
377 r = red() / 255.0;
378 g = green() / 255.0;
379 b = blue() / 255.0;
380 a = alpha() / 255.0;
381 }
382
383 void Color::getHSL(double& hue, double& saturation, double& lightness) const
384 {
385 // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of
386 // the algorithm therein, although it's 360^o based and we end up wanting
387 // [0...1) based. It's clearer if we stick to 360^o until the end.
388 double r = static_cast<double>(red()) / 255.0;
389 double g = static_cast<double>(green()) / 255.0;
390 double b = static_cast<double>(blue()) / 255.0;
391 double max = std::max(std::max(r, g), b);
392 double min = std::min(std::min(r, g), b);
393
394 if (max == min)
395 hue = 0.0;
396 else if (max == r)
397 hue = (60.0 * ((g - b) / (max - min))) + 360.0;
398 else if (max == g)
399 hue = (60.0 * ((b - r) / (max - min))) + 120.0;
400 else
401 hue = (60.0 * ((r - g) / (max - min))) + 240.0;
402
403 if (hue >= 360.0)
404 hue -= 360.0;
405
406 // makeRGBAFromHSLA assumes that hue is in [0...1).
407 hue /= 360.0;
408
409 lightness = 0.5 * (max + min);
410 if (max == min)
411 saturation = 0.0;
412 else if (lightness <= 0.5)
413 saturation = ((max - min) / (max + min));
414 else
415 saturation = ((max - min) / (2.0 - (max + min)));
416 }
417
418 Color colorFromPremultipliedARGB(RGBA32 pixelColor)
419 {
420 int alpha = alphaChannel(pixelColor);
421 if (alpha && alpha < 255) {
422 return Color::createUnchecked(
423 redChannel(pixelColor) * 255 / alpha,
424 greenChannel(pixelColor) * 255 / alpha,
425 blueChannel(pixelColor) * 255 / alpha,
426 alpha);
427 } else
428 return Color(pixelColor);
429 }
430
431 RGBA32 premultipliedARGBFromColor(const Color& color)
432 {
433 unsigned pixelColor;
434
435 unsigned alpha = color.alpha();
436 if (alpha < 255) {
437 pixelColor = Color::createUnchecked(
438 (color.red() * alpha + 254) / 255,
439 (color.green() * alpha + 254) / 255,
440 (color.blue() * alpha + 254) / 255,
441 alpha).rgb();
442 } else
443 pixelColor = color.rgb();
444
445 return pixelColor;
446 }
447
448 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/platform/graphics/Color.h ('k') | Source/core/platform/graphics/DashArray.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698