Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkColor.cpp
Go to the documentation of this file.
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
12#include "src/base/SkVx.h"
14
15#include <algorithm>
16
20
25
26///////////////////////////////////////////////////////////////////////////////
27
28static inline SkScalar ByteToScalar(U8CPU x) {
29 SkASSERT(x <= 255);
30 return SkIntToScalar(x) / 255;
31}
32
33static inline SkScalar ByteDivToScalar(int numer, U8CPU denom) {
34 // cast to keep the answer signed
35 return SkIntToScalar(numer) / (int)denom;
36}
37
38void 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
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
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;
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
113template <>
117 return rgba;
118}
119
120template <>
122 return Sk4f_toL32(swizzle_rb(skvx::float4::Load(this->vec())));
123}
124
125template <>
126uint32_t SkColor4f::toBytes_RGBA() const {
127 return Sk4f_toL32(skvx::float4::Load(this->vec()));
128}
129
130template <>
134 return color;
135}
136
137template <>
141 return color;
142}
143
144template <>
145uint32_t SkPMColor4f::toBytes_RGBA() const {
146 return Sk4f_toL32(skvx::float4::Load(this->vec()));
147}
148
149template <>
153 return color;
154}
SkColor4f color
static const uint32_t bgra[kNumPixels]
static const uint32_t rgba[kNumPixels]
#define SkASSERT(cond)
Definition SkAssert.h:116
unsigned U8CPU
Definition SkCPUTypes.h:18
static SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
static SkScalar ByteDivToScalar(int numer, U8CPU denom)
Definition SkColor.cpp:33
void SkRGBToHSV(U8CPU r, U8CPU g, U8CPU b, SkScalar hsv[3])
Definition SkColor.cpp:38
SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3])
Definition SkColor.cpp:78
SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.cpp:17
SkPMColor SkPreMultiplyColor(SkColor c)
Definition SkColor.cpp:21
static SkScalar ByteToScalar(U8CPU x)
Definition SkColor.cpp:28
#define SkColorGetR(color)
Definition SkColor.h:65
#define SkColorGetG(color)
Definition SkColor.h:69
uint32_t SkColor
Definition SkColor.h:37
uint32_t SkPMColor
Definition SkColor.h:205
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
#define SkColorGetA(color)
Definition SkColor.h:61
#define SkColorGetB(color)
Definition SkColor.h:73
#define SkScalarFloorToScalar(x)
Definition SkScalar.h:30
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:101
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkIntToScalar(x)
Definition SkScalar.h:57
static skvx::float4 swizzle_rb_if_bgra(const skvx::float4 &x)
static skvx::float4 Sk4f_fromL32(uint32_t px)
static uint32_t Sk4f_toL32(const skvx::float4 &px)
static skvx::float4 swizzle_rb(const skvx::float4 &x)
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
Type::kYUV Type::kRGBA() int(0.7 *637)
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct s
struct MyStruct a[10]
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
double x
SkScalar w
SkScalar h
const float * vec() const
Definition SkColor.h:308
static SkRGBA4f FromBytes_RGBA(uint32_t color)
static SkRGBA4f FromColor(SkColor color)
static SkRGBA4f FromPMColor(SkPMColor)
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition SkVx.h:109
SKVX_ALWAYS_INLINE void store(void *ptr) const
Definition SkVx.h:112