OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "wtf/FastAllocBase.h" | 30 #include "wtf/FastAllocBase.h" |
31 #include "wtf/Forward.h" | 31 #include "wtf/Forward.h" |
32 #include "wtf/unicode/Unicode.h" | 32 #include "wtf/unicode/Unicode.h" |
33 | 33 |
34 namespace WebCore { | 34 namespace WebCore { |
35 | 35 |
36 class Color; | 36 class Color; |
37 | 37 |
38 typedef unsigned RGBA32; // RGBA quadruplet | 38 typedef unsigned RGBA32; // RGBA quadruplet |
39 | 39 |
40 RGBA32 makeRGB(int r, int g, int b); | 40 PLATFORM_EXPORT RGBA32 makeRGB(int r, int g, int b); |
41 RGBA32 makeRGBA(int r, int g, int b, int a); | 41 PLATFORM_EXPORT RGBA32 makeRGBA(int r, int g, int b, int a); |
42 | 42 |
43 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha); | 43 PLATFORM_EXPORT RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha)
; |
44 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a); | 44 PLATFORM_EXPORT RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a); |
45 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a); | 45 PLATFORM_EXPORT RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a); |
46 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a); | 46 PLATFORM_EXPORT RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, flo
at a); |
47 | 47 |
48 int differenceSquared(const Color&, const Color&); | 48 PLATFORM_EXPORT int differenceSquared(const Color&, const Color&); |
49 | 49 |
50 inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; } | 50 inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; } |
51 inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; } | 51 inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; } |
52 inline int blueChannel(RGBA32 color) { return color & 0xFF; } | 52 inline int blueChannel(RGBA32 color) { return color & 0xFF; } |
53 inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; } | 53 inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; } |
54 | 54 |
55 class Color { | 55 class PLATFORM_EXPORT Color { |
56 WTF_MAKE_FAST_ALLOCATED; | 56 WTF_MAKE_FAST_ALLOCATED; |
57 public: | 57 public: |
58 Color() : m_color(0), m_valid(false) { } | 58 Color() : m_color(0), m_valid(false) { } |
59 Color(RGBA32 color, bool valid = true) : m_color(color), m_valid(valid) { AS
SERT(!m_color || m_valid); } | 59 Color(RGBA32 color, bool valid = true) : m_color(color), m_valid(valid) { AS
SERT(!m_color || m_valid); } |
60 Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { } | 60 Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { } |
61 Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(t
rue) { } | 61 Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(t
rue) { } |
62 // Color is currently limited to 32bit RGBA, perhaps some day we'll support
better colors | 62 // Color is currently limited to 32bit RGBA, perhaps some day we'll support
better colors |
63 Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r,
g, b, a)), m_valid(true) { } | 63 Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r,
g, b, a)), m_valid(true) { } |
64 // Creates a new color from the specific CMYK and alpha values. | 64 // Creates a new color from the specific CMYK and alpha values. |
65 Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMY
KA(c, m, y, k, a)), m_valid(true) { } | 65 Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMY
KA(c, m, y, k, a)), m_valid(true) { } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 inline bool operator==(const Color& a, const Color& b) | 129 inline bool operator==(const Color& a, const Color& b) |
130 { | 130 { |
131 return a.rgb() == b.rgb() && a.isValid() == b.isValid(); | 131 return a.rgb() == b.rgb() && a.isValid() == b.isValid(); |
132 } | 132 } |
133 | 133 |
134 inline bool operator!=(const Color& a, const Color& b) | 134 inline bool operator!=(const Color& a, const Color& b) |
135 { | 135 { |
136 return !(a == b); | 136 return !(a == b); |
137 } | 137 } |
138 | 138 |
139 Color colorFromPremultipliedARGB(RGBA32); | 139 PLATFORM_EXPORT Color colorFromPremultipliedARGB(RGBA32); |
140 RGBA32 premultipliedARGBFromColor(const Color&); | 140 PLATFORM_EXPORT RGBA32 premultipliedARGBFromColor(const Color&); |
141 | 141 |
142 inline Color blend(const Color& from, const Color& to, double progress, bool ble
ndPremultiplied = true) | 142 inline Color blend(const Color& from, const Color& to, double progress, bool ble
ndPremultiplied = true) |
143 { | 143 { |
144 // We need to preserve the state of the valid flag at the end of the animati
on | 144 // We need to preserve the state of the valid flag at the end of the animati
on |
145 if (progress == 1 && !to.isValid()) | 145 if (progress == 1 && !to.isValid()) |
146 return Color(); | 146 return Color(); |
147 | 147 |
148 if (blendPremultiplied) { | 148 if (blendPremultiplied) { |
149 // Contrary to the name, RGBA32 actually stores ARGB, so we can initiali
ze Color directly from premultipliedARGBFromColor(). | 149 // Contrary to the name, RGBA32 actually stores ARGB, so we can initiali
ze Color directly from premultipliedARGBFromColor(). |
150 // Also, premultipliedARGBFromColor() bails on zero alpha, so special-ca
se that. | 150 // Also, premultipliedARGBFromColor() bails on zero alpha, so special-ca
se that. |
151 Color premultFrom = from.alpha() ? premultipliedARGBFromColor(from) : 0; | 151 Color premultFrom = from.alpha() ? premultipliedARGBFromColor(from) : 0; |
152 Color premultTo = to.alpha() ? premultipliedARGBFromColor(to) : 0; | 152 Color premultTo = to.alpha() ? premultipliedARGBFromColor(to) : 0; |
153 | 153 |
154 Color premultBlended(blend(premultFrom.red(), premultTo.red(), progress)
, | 154 Color premultBlended(blend(premultFrom.red(), premultTo.red(), progress)
, |
155 blend(premultFrom.green(), premultTo.green(), progress), | 155 blend(premultFrom.green(), premultTo.green(), progress), |
156 blend(premultFrom.blue(), premultTo.blue(), progress), | 156 blend(premultFrom.blue(), premultTo.blue(), progress), |
157 blend(premultFrom.alpha(), premultTo.alpha(), progress)); | 157 blend(premultFrom.alpha(), premultTo.alpha(), progress)); |
158 | 158 |
159 return Color(colorFromPremultipliedARGB(premultBlended.rgb())); | 159 return Color(colorFromPremultipliedARGB(premultBlended.rgb())); |
160 } | 160 } |
161 | 161 |
162 return Color(blend(from.red(), to.red(), progress), | 162 return Color(blend(from.red(), to.red(), progress), |
163 blend(from.green(), to.green(), progress), | 163 blend(from.green(), to.green(), progress), |
164 blend(from.blue(), to.blue(), progress), | 164 blend(from.blue(), to.blue(), progress), |
165 blend(from.alpha(), to.alpha(), progress)); | 165 blend(from.alpha(), to.alpha(), progress)); |
166 } | 166 } |
167 } // namespace WebCore | 167 } // namespace WebCore |
168 | 168 |
169 #endif // Color_h | 169 #endif // Color_h |
OLD | NEW |