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