| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 namespace { // See SkPMFloat.h | 8 namespace { // See SkPMFloat.h |
| 9 | 9 |
| 10 inline SkPMFloat::SkPMFloat(SkPMColor c) { | 10 template <int kBias> |
| 11 float inv255 = 1.0f/255; | 11 inline SkPMFloat<kBias>::SkPMFloat(SkPMColor c) { |
| 12 *this = SkPMFloat::FromARGB(SkGetPackedA32(c) * inv255, | 12 float scale = (kBias == 1) ? (1.0f/255) : 1; |
| 13 SkGetPackedR32(c) * inv255, | 13 *this = SkPMFloat::FromARGB(SkGetPackedA32(c) * scale, |
| 14 SkGetPackedG32(c) * inv255, | 14 SkGetPackedR32(c) * scale, |
| 15 SkGetPackedB32(c) * inv255); | 15 SkGetPackedG32(c) * scale, |
| 16 SkGetPackedB32(c) * scale); |
| 16 SkASSERT(this->isValid()); | 17 SkASSERT(this->isValid()); |
| 17 } | 18 } |
| 18 | 19 |
| 19 inline SkPMColor SkPMFloat::round() const { | 20 template <int kBias> |
| 21 inline SkPMColor SkPMFloat<kBias>::round() const { |
| 22 float scale = (kBias == 1) ? 255 : 1; |
| 20 float a = this->a(), | 23 float a = this->a(), |
| 21 r = this->r(), | 24 r = this->r(), |
| 22 g = this->g(), | 25 g = this->g(), |
| 23 b = this->b(); | 26 b = this->b(); |
| 24 a = a < 0 ? 0 : (a > 1 ? 1 : a); | 27 a = a < 0 ? 0 : (a > 1 ? 1 : a); |
| 25 r = r < 0 ? 0 : (r > 1 ? 1 : r); | 28 r = r < 0 ? 0 : (r > 1 ? 1 : r); |
| 26 g = g < 0 ? 0 : (g > 1 ? 1 : g); | 29 g = g < 0 ? 0 : (g > 1 ? 1 : g); |
| 27 b = b < 0 ? 0 : (b > 1 ? 1 : b); | 30 b = b < 0 ? 0 : (b > 1 ? 1 : b); |
| 28 SkPMColor c = SkPackARGB32(255*a+0.5f, 255*r+0.5f, 255*g+0.5f, 255*b+0.5f); | 31 SkPMColor c = SkPackARGB32(scale*a+0.5f, scale*r+0.5f, scale*g+0.5f, scale*b
+0.5f); |
| 29 SkPMColorAssert(c); | 32 SkPMColorAssert(c); |
| 30 return c; | 33 return c; |
| 31 } | 34 } |
| 32 | 35 |
| 33 inline Sk4f SkPMFloat::alphas() const { | 36 template <int kBias> |
| 37 inline Sk4f SkPMFloat<kBias>::alphas() const { |
| 34 return Sk4f(this->a()); | 38 return Sk4f(this->a()); |
| 35 } | 39 } |
| 36 | 40 |
| 37 inline SkPMFloat SkPMFloat::FromOpaqueColor(SkColor c) { | 41 template <int kBias> |
| 42 inline SkPMFloat<kBias> SkPMFloat<kBias>::FromOpaqueColor(SkColor c) { |
| 38 SkASSERT(SkColorGetA(c) == 0xFF); | 43 SkASSERT(SkColorGetA(c) == 0xFF); |
| 39 float inv255 = 1.0f / 255; | 44 float scale = (kBias == 1) ? (1.0f/255) : 1; |
| 40 SkPMFloat pmf = SkPMFloat::FromARGB(1.0f, | 45 SkPMFloat pmf = SkPMFloat::FromARGB(kBias, |
| 41 SkColorGetR(c) * inv255, | 46 SkColorGetR(c) * scale, |
| 42 SkColorGetG(c) * inv255, | 47 SkColorGetG(c) * scale, |
| 43 SkColorGetB(c) * inv255); | 48 SkColorGetB(c) * scale); |
| 44 SkASSERT(pmf.isValid()); | 49 SkASSERT(pmf.isValid()); |
| 45 return pmf; | 50 return pmf; |
| 46 } | 51 } |
| 47 | 52 |
| 48 } // namespace | 53 } // namespace |
| OLD | NEW |