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
12SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
13 return SkPremultiplyARGBInline(a, r, g, b);
14}
15
16SkPMColor SkPreMultiplyColor(SkColor c) {
17 return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c),
18 SkColorGetG(c), SkColorGetB(c));
19}
20
21///////////////////////////////////////////////////////////////////////////////
22
23static inline SkScalar ByteToScalar(U8CPU x) {
24 SkASSERT(x <= 255);
25 return SkIntToScalar(x) / 255;
26}
27
28static inline SkScalar ByteDivToScalar(int numer, U8CPU denom) {
29 // cast to keep the answer signed
30 return SkIntToScalar(numer) / (int)denom;
31}
32
33void SkRGBToHSV(U8CPU r, U8CPU g, U8CPU b, SkScalar hsv[3]) {
34 SkASSERT(hsv);
35
36 unsigned min = std::min(r, std::min(g, b));
37 unsigned max = std::max(r, std::max(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
73SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) {
74 SkASSERT(hsv);
75
76 SkScalar s = SkTPin(hsv[1], 0.0f, 1.0f);
77 SkScalar v = SkTPin(hsv[2], 0.0f, 1.0f);
78
79 U8CPU v_byte = SkScalarRoundToInt(v * 255);
80
81 if (SkScalarNearlyZero(s)) { // shade of gray
82 return SkColorSetARGB(a, v_byte, v_byte, v_byte);
83 }
84 SkScalar hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : hsv[0]/60;
85 SkScalar w = SkScalarFloorToScalar(hx);
86 SkScalar f = hx - w;
87
88 unsigned p = SkScalarRoundToInt((SK_Scalar1 - s) * v * 255);
89 unsigned q = SkScalarRoundToInt((SK_Scalar1 - (s * f)) * v * 255);
90 unsigned t = SkScalarRoundToInt((SK_Scalar1 - (s * (SK_Scalar1 - f))) * v * 255);
91
92 unsigned r, g, b;
93
94 SkASSERT((unsigned)(w) < 6);
95 switch ((unsigned)(w)) {
96 case 0: r = v_byte; g = t; b = p; break;
97 case 1: r = q; g = v_byte; b = p; break;
98 case 2: r = p; g = v_byte; b = t; break;
99 case 3: r = p; g = q; b = v_byte; break;
100 case 4: r = t; g = p; b = v_byte; break;
101 default: r = v_byte; g = p; b = q; break;
102 }
103 return SkColorSetARGB(a, r, g, b);
104}
105
106///////////////////////////////////////////////////////////////////////////////////////////////////
107
108template <>
109SkColor4f SkColor4f::FromColor(SkColor bgra) {
110 SkColor4f rgba;
111 swizzle_rb(Sk4f_fromL32(bgra)).store(rgba.vec());
112 return rgba;
113}
114
115template <>
116SkColor SkColor4f::toSkColor() const {
117 return Sk4f_toL32(swizzle_rb(Sk4f::Load(this->vec())));
118}
119
120template <>
121uint32_t SkColor4f::toBytes_RGBA() const {
122 return Sk4f_toL32(Sk4f::Load(this->vec()));
123}
124
125template <>
126SkColor4f SkColor4f::FromBytes_RGBA(uint32_t c) {
127 SkColor4f color;
128 Sk4f_fromL32(c).store(&color);
129 return color;
130}
131
132template <>
133SkPMColor4f SkPMColor4f::FromPMColor(SkPMColor c) {
134 SkPMColor4f color;
135 swizzle_rb_if_bgra(Sk4f_fromL32(c)).store(&color);
136 return color;
137}
138
139template <>
140uint32_t SkPMColor4f::toBytes_RGBA() const {
141 return Sk4f_toL32(Sk4f::Load(this->vec()));
142}
143
144template <>
145SkPMColor4f SkPMColor4f::FromBytes_RGBA(uint32_t c) {
146 SkPMColor4f color;
147 Sk4f_fromL32(c).store(&color);
148 return color;
149}
150