1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkColor.h"
9 #include "include/core/SkColorPriv.h"
10 #include "include/private/SkColorData.h"
11 #include "include/private/base/SkTPin.h"
12 #include "src/base/SkVx.h"
13 #include "src/core/SkSwizzlePriv.h"
14
15 #include <algorithm>
16
SkPreMultiplyARGB(U8CPU a,U8CPU r,U8CPU g,U8CPU b)17 SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
18 return SkPremultiplyARGBInline(a, r, g, b);
19 }
20
SkPreMultiplyColor(SkColor c)21 SkPMColor SkPreMultiplyColor(SkColor c) {
22 return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c),
23 SkColorGetG(c), SkColorGetB(c));
24 }
25
26 ///////////////////////////////////////////////////////////////////////////////
27
ByteToScalar(U8CPU x)28 static inline SkScalar ByteToScalar(U8CPU x) {
29 SkASSERT(x <= 255);
30 return SkIntToScalar(x) / 255;
31 }
32
ByteDivToScalar(int numer,U8CPU denom)33 static inline SkScalar ByteDivToScalar(int numer, U8CPU denom) {
34 // cast to keep the answer signed
35 return SkIntToScalar(numer) / (int)denom;
36 }
37
SkRGBToHSV(U8CPU r,U8CPU g,U8CPU b,SkScalar hsv[3])38 void SkRGBToHSV(U8CPU r, U8CPU g, U8CPU b, SkScalar hsv[3]) {
39 SkASSERT(hsv);
40
41 unsigned min = std::min(r, std::min(g, b));
42 unsigned max = std::max(r, std::max(g, b));
43 unsigned delta = max - min;
44
45 SkScalar v = ByteToScalar(max);
46 SkASSERT(v >= 0 && v <= SK_Scalar1);
47
48 if (0 == delta) { // we're a shade of gray
49 hsv[0] = 0;
50 hsv[1] = 0;
51 hsv[2] = v;
52 return;
53 }
54
55 SkScalar s = ByteDivToScalar(delta, max);
56 SkASSERT(s >= 0 && s <= SK_Scalar1);
57
58 SkScalar h;
59 if (r == max) {
60 h = ByteDivToScalar(g - b, delta);
61 } else if (g == max) {
62 h = SkIntToScalar(2) + ByteDivToScalar(b - r, delta);
63 } else { // b == max
64 h = SkIntToScalar(4) + ByteDivToScalar(r - g, delta);
65 }
66
67 h *= 60;
68 if (h < 0) {
69 h += SkIntToScalar(360);
70 }
71 SkASSERT(h >= 0 && h < SkIntToScalar(360));
72
73 hsv[0] = h;
74 hsv[1] = s;
75 hsv[2] = v;
76 }
77
SkHSVToColor(U8CPU a,const SkScalar hsv[3])78 SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) {
79 SkASSERT(hsv);
80
81 SkScalar s = SkTPin(hsv[1], 0.0f, 1.0f);
82 SkScalar v = SkTPin(hsv[2], 0.0f, 1.0f);
83
84 U8CPU v_byte = SkScalarRoundToInt(v * 255);
85
86 if (SkScalarNearlyZero(s)) { // shade of gray
87 return SkColorSetARGB(a, v_byte, v_byte, v_byte);
88 }
89 SkScalar hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : hsv[0]/60;
90 SkScalar w = SkScalarFloorToScalar(hx);
91 SkScalar f = hx - w;
92
93 unsigned p = SkScalarRoundToInt((SK_Scalar1 - s) * v * 255);
94 unsigned q = SkScalarRoundToInt((SK_Scalar1 - (s * f)) * v * 255);
95 unsigned t = SkScalarRoundToInt((SK_Scalar1 - (s * (SK_Scalar1 - f))) * v * 255);
96
97 unsigned r, g, b;
98
99 SkASSERT((unsigned)(w) < 6);
100 switch ((unsigned)(w)) {
101 case 0: r = v_byte; g = t; b = p; break;
102 case 1: r = q; g = v_byte; b = p; break;
103 case 2: r = p; g = v_byte; b = t; break;
104 case 3: r = p; g = q; b = v_byte; break;
105 case 4: r = t; g = p; b = v_byte; break;
106 default: r = v_byte; g = p; b = q; break;
107 }
108 return SkColorSetARGB(a, r, g, b);
109 }
110
111 ///////////////////////////////////////////////////////////////////////////////////////////////////
112
113 template <>
FromColor(SkColor bgra)114 SkColor4f SkColor4f::FromColor(SkColor bgra) {
115 SkColor4f rgba;
116 swizzle_rb(Sk4f_fromL32(bgra)).store(rgba.vec());
117 return rgba;
118 }
119
120 template <>
toSkColor() const121 SkColor SkColor4f::toSkColor() const {
122 return Sk4f_toL32(swizzle_rb(skvx::float4::Load(this->vec())));
123 }
124
125 template <>
toBytes_RGBA() const126 uint32_t SkColor4f::toBytes_RGBA() const {
127 return Sk4f_toL32(skvx::float4::Load(this->vec()));
128 }
129
130 template <>
FromBytes_RGBA(uint32_t c)131 SkColor4f SkColor4f::FromBytes_RGBA(uint32_t c) {
132 SkColor4f color;
133 Sk4f_fromL32(c).store(&color);
134 return color;
135 }
136
137 template <>
FromPMColor(SkPMColor c)138 SkPMColor4f SkPMColor4f::FromPMColor(SkPMColor c) {
139 SkPMColor4f color;
140 swizzle_rb_if_bgra(Sk4f_fromL32(c)).store(&color);
141 return color;
142 }
143
144 template <>
toBytes_RGBA() const145 uint32_t SkPMColor4f::toBytes_RGBA() const {
146 return Sk4f_toL32(skvx::float4::Load(this->vec()));
147 }
148
149 template <>
FromBytes_RGBA(uint32_t c)150 SkPMColor4f SkPMColor4f::FromBytes_RGBA(uint32_t c) {
151 SkPMColor4f color;
152 Sk4f_fromL32(c).store(&color);
153 return color;
154 }
155