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 | |
| 12 | SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
reed@google.com | 0b8b3bb | 2011-06-30 21:06:22 +0000 | [diff] [blame] | 13 | return SkPremultiplyARGBInline(a, r, g, b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | SkPMColor SkPreMultiplyColor(SkColor c) { |
reed@google.com | 0b8b3bb | 2011-06-30 21:06:22 +0000 | [diff] [blame] | 17 | return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c), |
| 18 | SkColorGetG(c), SkColorGetB(c)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | } |
| 20 | |
reed@google.com | 0b8b3bb | 2011-06-30 21:06:22 +0000 | [diff] [blame] | 21 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
| 23 | static inline SkScalar ByteToScalar(U8CPU x) { |
| 24 | SkASSERT(x <= 255); |
| 25 | return SkIntToScalar(x) / 255; |
| 26 | } |
| 27 | |
| 28 | static inline SkScalar ByteDivToScalar(int numer, U8CPU denom) { |
| 29 | // cast to keep the answer signed |
| 30 | return SkIntToScalar(numer) / (int)denom; |
| 31 | } |
| 32 | |
| 33 | void SkRGBToHSV(U8CPU r, U8CPU g, U8CPU b, SkScalar hsv[3]) { |
| 34 | SkASSERT(hsv); |
| 35 | |
| 36 | unsigned min = SkMin32(r, SkMin32(g, b)); |
| 37 | unsigned max = SkMax32(r, SkMax32(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 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 53 | SkScalar h; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) { |
| 74 | SkASSERT(hsv); |
| 75 | |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 76 | U8CPU s = SkUnitScalarClampToByte(hsv[1]); |
| 77 | U8CPU v = SkUnitScalarClampToByte(hsv[2]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | if (0 == s) { // shade of gray |
| 80 | return SkColorSetARGB(a, v, v, v); |
| 81 | } |
| 82 | SkFixed hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : SkScalarToFixed(hsv[0]/60); |
| 83 | SkFixed f = hx & 0xFFFF; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | unsigned v_scale = SkAlpha255To256(v); |
| 86 | unsigned p = SkAlphaMul(255 - s, v_scale); |
| 87 | unsigned q = SkAlphaMul(255 - (s * f >> 16), v_scale); |
| 88 | unsigned t = SkAlphaMul(255 - (s * (SK_Fixed1 - f) >> 16), v_scale); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | unsigned r, g, b; |
| 91 | |
| 92 | SkASSERT((unsigned)(hx >> 16) < 6); |
| 93 | switch (hx >> 16) { |
| 94 | case 0: r = v; g = t; b = p; break; |
| 95 | case 1: r = q; g = v; b = p; break; |
| 96 | case 2: r = p; g = v; b = t; break; |
| 97 | case 3: r = p; g = q; b = v; break; |
| 98 | case 4: r = t; g = p; b = v; break; |
| 99 | default: r = v; g = p; b = q; break; |
| 100 | } |
| 101 | return SkColorSetARGB(a, r, g, b); |
| 102 | } |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 103 | |
| 104 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | #include "SkNx.h" |
| 106 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame^] | 107 | SkPM4f SkPM4f::FromPMColor(SkPMColor c) { |
| 108 | Sk4f value = SkNx_cast<float>(Sk4b::Load((const uint8_t*)&c)); |
| 109 | SkPM4f c4; |
| 110 | (value * Sk4f(1.0f / 255)).store(c4.fVec); |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 111 | return c4; |
| 112 | } |
| 113 | |
| 114 | SkColor4f SkColor4f::FromColor(SkColor c) { |
| 115 | Sk4f value = SkNx_shuffle<3,2,1,0>(SkNx_cast<float>(Sk4b::Load((const uint8_t*)&c))); |
| 116 | SkColor4f c4; |
| 117 | (value * Sk4f(1.0f / 255)).store(c4.vec()); |
| 118 | return c4; |
| 119 | } |
| 120 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame^] | 121 | SkColor4f SkColor4f::Pin(float a, float r, float g, float b) { |
| 122 | SkColor4f c4; |
| 123 | Sk4f::Min(Sk4f::Max(Sk4f(a, r, g, b), Sk4f(0)), Sk4f(1)).store(c4.vec()); |
| 124 | return c4; |
| 125 | } |
| 126 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 127 | SkPM4f SkColor4f::premul() const { |
| 128 | auto src = Sk4f::Load(this->pin().vec()); |
| 129 | float srcAlpha = src.kth<0>(); // need the pinned version of our alpha |
| 130 | src = src * Sk4f(1, srcAlpha, srcAlpha, srcAlpha); |
| 131 | |
| 132 | #ifdef SK_PMCOLOR_IS_BGRA |
| 133 | // ARGB -> BGRA |
| 134 | Sk4f dst = SkNx_shuffle<3,2,1,0>(src); |
| 135 | #else |
| 136 | // ARGB -> RGBA |
| 137 | Sk4f dst = SkNx_shuffle<1,2,3,0>(src); |
| 138 | #endif |
| 139 | |
| 140 | SkPM4f pm4; |
| 141 | dst.store(pm4.fVec); |
| 142 | return pm4; |
| 143 | } |