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 | } |