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