1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #include "SkColor.h"
11 #include "SkColorPriv.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 = SkMin32(r, SkMin32(g, b));
38 unsigned max = SkMax32(r, SkMax32(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
UnitScalarToByte(SkScalar x)74 static inline U8CPU UnitScalarToByte(SkScalar x) {
75 if (x < 0) {
76 return 0;
77 }
78 if (x >= SK_Scalar1) {
79 return 255;
80 }
81 return SkScalarToFixed(x) >> 8;
82 }
83
SkHSVToColor(U8CPU a,const SkScalar hsv[3])84 SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) {
85 SkASSERT(hsv);
86
87 U8CPU s = UnitScalarToByte(hsv[1]);
88 U8CPU v = UnitScalarToByte(hsv[2]);
89
90 if (0 == s) { // shade of gray
91 return SkColorSetARGB(a, v, v, v);
92 }
93 SkFixed hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : SkScalarToFixed(hsv[0]/60);
94 SkFixed f = hx & 0xFFFF;
95
96 unsigned v_scale = SkAlpha255To256(v);
97 unsigned p = SkAlphaMul(255 - s, v_scale);
98 unsigned q = SkAlphaMul(255 - (s * f >> 16), v_scale);
99 unsigned t = SkAlphaMul(255 - (s * (SK_Fixed1 - f) >> 16), v_scale);
100
101 unsigned r, g, b;
102
103 SkASSERT((unsigned)(hx >> 16) < 6);
104 switch (hx >> 16) {
105 case 0: r = v; g = t; b = p; break;
106 case 1: r = q; g = v; b = p; break;
107 case 2: r = p; g = v; b = t; break;
108 case 3: r = p; g = q; b = v; break;
109 case 4: r = t; g = p; b = v; break;
110 default: r = v; g = p; b = q; break;
111 }
112 return SkColorSetARGB(a, r, g, b);
113 }
114