OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/skia_utils.h" | 5 #include "skia/ext/skia_utils.h" |
6 #include "skia/include/SkColorPriv.h" | |
7 | 6 |
8 #include "SkGradientShader.h" | 7 #include "SkGradientShader.h" |
9 | 8 |
10 namespace skia { | 9 namespace skia { |
11 | 10 |
12 SkShader* CreateGradientShader(int start_point, | 11 SkShader* CreateGradientShader(int start_point, |
13 int end_point, | 12 int end_point, |
14 SkColor start_color, | 13 SkColor start_color, |
15 SkColor end_color) { | 14 SkColor end_color) { |
16 SkColor grad_colors[2] = { start_color, end_color}; | 15 SkColor grad_colors[2] = { start_color, end_color}; |
17 SkPoint grad_points[2]; | 16 SkPoint grad_points[2]; |
18 grad_points[0].set(SkIntToScalar(0), SkIntToScalar(start_point)); | 17 grad_points[0].set(SkIntToScalar(0), SkIntToScalar(start_point)); |
19 grad_points[1].set(SkIntToScalar(0), SkIntToScalar(end_point)); | 18 grad_points[1].set(SkIntToScalar(0), SkIntToScalar(end_point)); |
20 | 19 |
21 return SkGradientShader::CreateLinear( | 20 return SkGradientShader::CreateLinear( |
22 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode); | 21 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode); |
23 } | 22 } |
24 | 23 |
25 // Helper function for HSLToSKColor. | |
26 static inline double calcHue(double temp1, double temp2, double hueVal) { | |
27 if (hueVal < 0.0) | |
28 hueVal++; | |
29 else if (hueVal > 1.0) | |
30 hueVal--; | |
31 | |
32 if (hueVal * 6.0 < 1.0) | |
33 return temp1 + (temp2 - temp1) * hueVal * 6.0; | |
34 if (hueVal * 2.0 < 1.0) | |
35 return temp2; | |
36 if (hueVal * 3.0 < 2.0) | |
37 return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0; | |
38 | |
39 return temp1; | |
40 } | |
41 | |
42 SkPMColor HSLToSKColor(U8CPU alpha, float hsl[3]) { | |
43 double hue = SkScalarToDouble(hsl[0]); | |
44 double saturation = SkScalarToDouble(hsl[1]); | |
45 double lightness = SkScalarToDouble(hsl[2]); | |
46 double scaleFactor = 256.0; | |
47 | |
48 // If there's no color, we don't care about hue and can do everything based | |
49 // on brightness. | |
50 if (!saturation) { | |
51 U8CPU lightness; | |
52 | |
53 if (hsl[2] < 0) | |
54 lightness = 0; | |
55 else if (hsl[2] >= SK_Scalar1) | |
56 lightness = 255; | |
57 else | |
58 lightness = SkScalarToFixed(hsl[2]) >> 8; | |
59 | |
60 unsigned greyValue = SkAlphaMul(lightness, alpha); | |
61 return SkColorSetARGB(alpha, greyValue, greyValue, greyValue); | |
62 } | |
63 | |
64 double temp2 = (lightness < 0.5) ? | |
65 lightness * (1.0 + saturation) : | |
66 lightness + saturation - (lightness * saturation); | |
67 double temp1 = 2.0 * lightness - temp2; | |
68 | |
69 double rh = calcHue(temp1, temp2, hue + 1.0 / 3.0); | |
70 double gh = calcHue(temp1, temp2, hue); | |
71 double bh = calcHue(temp1, temp2, hue - 1.0 / 3.0); | |
72 | |
73 return SkColorSetARGB(alpha, | |
74 SkAlphaMul(static_cast<int>(rh * scaleFactor), alpha), | |
75 SkAlphaMul(static_cast<int>(gh * scaleFactor), alpha), | |
76 SkAlphaMul(static_cast<int>(bh * scaleFactor), alpha)); | |
77 } | |
78 | |
79 void SkColorToHSL(SkPMColor c, float hsl[3]) { | |
80 double r = SkColorGetR(c) / 255.0; | |
81 double g = SkColorGetG(c) / 255.0; | |
82 double b = SkColorGetB(c) / 255.0; | |
83 | |
84 double h, s, l; | |
85 | |
86 double vmax = r > g ? r : g; | |
87 vmax = vmax > b ? vmax : b; | |
88 double vmin = r < g ? r : g; | |
89 vmin = vmin < b ? vmin : b; | |
90 double delta = vmax - vmin; | |
91 | |
92 l = (vmax + vmin) / 2; | |
93 | |
94 if (delta == 0) { | |
95 h = 0; | |
96 s = 0; | |
97 } else { | |
98 if (l < 0.5) | |
99 s = delta / (vmax + vmin); | |
100 else | |
101 s = delta / (2 - vmax - vmin); | |
102 | |
103 double dr = (((vmax - r) / 6.0) + (delta / 2.0)) / delta; | |
104 double dg = (((vmax - g) / 6.0) + (delta / 2.0)) / delta; | |
105 double db = (((vmax - b) / 6.0) + (delta / 2.0)) / delta; | |
106 | |
107 if (r == vmax) | |
108 h = db - dg; | |
109 else if (g == vmax) | |
110 h = (1.0 / 3.0) + dr - db; | |
111 else if (b == vmax) | |
112 h = (2.0 / 3.0) + dg - dr; | |
113 | |
114 if (h < 0) h += 1; | |
115 if (h > 1) h -= 1; | |
116 } | |
117 | |
118 hsl[0] = h; | |
119 hsl[1] = s; | |
120 hsl[2] = l; | |
121 } | |
122 | |
123 | |
124 } // namespace skia | 24 } // namespace skia |
125 | 25 |
OLD | NEW |