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 "SkColor.h"
9 #include "SkColorPriv.h"
10 #include "SkFixed.h"
11
SkPreMultiplyARGB(U8CPU a,U8CPU r,U8CPU g,U8CPU b)12 SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
13 return SkPremultiplyARGBInline(a, r, g, b);
14 }
15
SkPreMultiplyColor(SkColor c)16 SkPMColor SkPreMultiplyColor(SkColor c) {
17 return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c),
18 SkColorGetG(c), SkColorGetB(c));
19 }
20
21 ///////////////////////////////////////////////////////////////////////////////
22
ByteToScalar(U8CPU x)23 static inline SkScalar ByteToScalar(U8CPU x) {
24 SkASSERT(x <= 255);
25 return SkIntToScalar(x) / 255;
26 }
27
ByteDivToScalar(int numer,U8CPU denom)28 static inline SkScalar ByteDivToScalar(int numer, U8CPU denom) {
29 // cast to keep the answer signed
30 return SkIntToScalar(numer) / (int)denom;
31 }
32
SkRGBToHSV(U8CPU r,U8CPU g,U8CPU b,SkScalar hsv[3])33 void SkRGBToHSV(U8CPU r, U8CPU g, U8CPU b, SkScalar hsv[3]) {
34 SkASSERT(hsv);
35
36 unsigned min = SkMin32(r, SkMin32(g, b));
37 unsigned max = SkMax32(r, SkMax32(g, b));
38 unsigned delta = max - min;
39
40 SkScalar v = ByteToScalar(max);
41 SkASSERT(v >= 0 && v <= SK_Scalar1);
42
43 if (0 == delta) { // we're a shade of gray
44 hsv[0] = 0;
45 hsv[1] = 0;
46 hsv[2] = v;
47 return;
48 }
49
50 SkScalar s = ByteDivToScalar(delta, max);
51 SkASSERT(s >= 0 && s <= SK_Scalar1);
52
53 SkScalar h;
54 if (r == max) {
55 h = ByteDivToScalar(g - b, delta);
56 } else if (g == max) {
57 h = SkIntToScalar(2) + ByteDivToScalar(b - r, delta);
58 } else { // b == max
59 h = SkIntToScalar(4) + ByteDivToScalar(r - g, delta);
60 }
61
62 h *= 60;
63 if (h < 0) {
64 h += SkIntToScalar(360);
65 }
66 SkASSERT(h >= 0 && h < SkIntToScalar(360));
67
68 hsv[0] = h;
69 hsv[1] = s;
70 hsv[2] = v;
71 }
72
SkHSVToColor(U8CPU a,const SkScalar hsv[3])73 SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) {
74 SkASSERT(hsv);
75
76 U8CPU s = SkUnitScalarClampToByte(hsv[1]);
77 U8CPU v = SkUnitScalarClampToByte(hsv[2]);
78
79 if (0 == s) { // shade of gray
80 return SkColorSetARGB(a, v, v, v);
81 }
82 SkFixed hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : SkScalarToFixed(hsv[0]/60);
83 SkFixed f = hx & 0xFFFF;
84
85 unsigned v_scale = SkAlpha255To256(v);
86 unsigned p = SkAlphaMul(255 - s, v_scale);
87 unsigned q = SkAlphaMul(255 - (s * f >> 16), v_scale);
88 unsigned t = SkAlphaMul(255 - (s * (SK_Fixed1 - f) >> 16), v_scale);
89
90 unsigned r, g, b;
91
92 SkASSERT((unsigned)(hx >> 16) < 6);
93 switch (hx >> 16) {
94 case 0: r = v; g = t; b = p; break;
95 case 1: r = q; g = v; b = p; break;
96 case 2: r = p; g = v; b = t; break;
97 case 3: r = p; g = q; b = v; break;
98 case 4: r = t; g = p; b = v; break;
99 default: r = v; g = p; b = q; break;
100 }
101 return SkColorSetARGB(a, r, g, b);
102 }
103
104 ///////////////////////////////////////////////////////////////////////////////////////////////////
105 #include "SkPM4f.h"
106 #include "SkNx.h"
107 #include "SkHalf.h"
108
FromPMColor(SkPMColor c)109 SkPM4f SkPM4f::FromPMColor(SkPMColor c) {
110 Sk4f value = SkNx_cast<float>(Sk4b::Load(&c));
111 SkPM4f c4;
112 (value * Sk4f(1.0f / 255)).store(&c4);
113 return c4;
114 }
115
unpremul() const116 SkColor4f SkPM4f::unpremul() const {
117 float alpha = fVec[A];
118 if (0 == alpha) {
119 return { 0, 0, 0, 0 };
120 } else {
121 float invAlpha = 1 / alpha;
122 return { alpha, fVec[R] * invAlpha, fVec[G] * invAlpha, fVec[B] * invAlpha };
123 }
124 }
125
toF16(uint16_t half[4]) const126 void SkPM4f::toF16(uint16_t half[4]) const {
127 for (int i = 0; i < 4; ++i) {
128 half[i] = SkFloatToHalf(fVec[i]);
129 }
130 }
131
toF16() const132 uint64_t SkPM4f::toF16() const {
133 uint64_t value;
134 this->toF16(reinterpret_cast<uint16_t*>(&value));
135 return value;
136 }
137
FromF16(const uint16_t half[4])138 SkPM4f SkPM4f::FromF16(const uint16_t half[4]) {
139 return {{
140 SkHalfToFloat(half[0]),
141 SkHalfToFloat(half[1]),
142 SkHalfToFloat(half[2]),
143 SkHalfToFloat(half[3])
144 }};
145 }
146
147 #ifdef SK_DEBUG
assertIsUnit() const148 void SkPM4f::assertIsUnit() const {
149 auto c4 = Sk4f::Load(fVec);
150 SkASSERT((c4 >= Sk4f(0)).allTrue() && (c4 <= Sk4f(1)).allTrue());
151 }
152 #endif
153
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
155
FromColor(SkColor c)156 SkColor4f SkColor4f::FromColor(SkColor c) {
157 Sk4f value = SkNx_shuffle<3,2,1,0>(SkNx_cast<float>(Sk4b::Load(&c)));
158 SkColor4f c4;
159 (value * Sk4f(1.0f / 255)).store(&c4);
160 return c4;
161 }
162
Pin(float a,float r,float g,float b)163 SkColor4f SkColor4f::Pin(float a, float r, float g, float b) {
164 SkColor4f c4;
165 Sk4f::Min(Sk4f::Max(Sk4f(a, r, g, b), Sk4f(0)), Sk4f(1)).store(c4.vec());
166 return c4;
167 }
168
premul() const169 SkPM4f SkColor4f::premul() const {
170 auto src = Sk4f::Load(this->pin().vec());
171 float srcAlpha = src[0]; // need the pinned version of our alpha
172 src = src * Sk4f(1, srcAlpha, srcAlpha, srcAlpha);
173
174 #ifdef SK_PMCOLOR_IS_BGRA
175 // ARGB -> BGRA
176 Sk4f dst = SkNx_shuffle<3,2,1,0>(src);
177 #else
178 // ARGB -> RGBA
179 Sk4f dst = SkNx_shuffle<1,2,3,0>(src);
180 #endif
181
182 SkPM4f pm4;
183 dst.store(&pm4);
184 return pm4;
185 }
186