OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 Google Inc. | |
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 | |
8 #ifndef SkPM_DEFINED | |
9 #define SkPM_DEFINED | |
10 | |
11 #include "SkTypes.h" | |
12 #include "SkColor.h" | |
13 #include "SkColorPriv.h" | |
14 #include "SkNx.h" | |
15 | |
16 // This file may be included multiple times by .cpp files with different flags,
leading | |
17 // to different definitions. Usually that doesn't matter because it's all inlin
ed, but | |
18 // in Debug modes the compilers may not inline everything. So wrap everything i
n an | |
19 // anonymous namespace to give each includer their own silo of this code (or the
linker | |
20 // will probably pick one randomly for us, which is rarely correct). | |
21 namespace { | |
22 | |
23 // A pre-multiplied color storing each component in the same order as SkPMColor, | |
24 // but as a float in the range [0, 1]. | |
25 class SkPMFloat : public Sk4f { | |
26 public: | |
27 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } | |
28 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF
loat(a,r,g,b); } | |
29 static SkPMFloat FromOpaqueColor(SkColor c); // Requires c's alpha == 0xFF. | |
30 | |
31 Sk4f alphas() const { return Sk4f(this->a()); } | |
32 | |
33 // Uninitialized. | |
34 SkPMFloat() {} | |
35 explicit SkPMFloat(SkPMColor c) { *this = Sk4f::FromBytes((uint8_t*)&c) * Sk
4f(1.0f/255); } | |
36 SkPMFloat(float a, float r, float g, float b) | |
37 #ifdef SK_PMCOLOR_IS_RGBA | |
38 : INHERITED(r,g,b,a) {} | |
39 #else | |
40 : INHERITED(b,g,r,a) {} | |
41 #endif | |
42 | |
43 SkPMFloat(const Sk4f& fs) : INHERITED(fs) {} | |
44 | |
45 float a() const { return this->kth<SK_A32_SHIFT / 8>(); } | |
46 float r() const { return this->kth<SK_R32_SHIFT / 8>(); } | |
47 float g() const { return this->kth<SK_G32_SHIFT / 8>(); } | |
48 float b() const { return this->kth<SK_B32_SHIFT / 8>(); } | |
49 | |
50 SkPMColor round() const { | |
51 SkPMColor c; | |
52 (*this * Sk4f(255) + Sk4f(0.5f)).toBytes((uint8_t*)&c); | |
53 return c; | |
54 } | |
55 | |
56 bool isValid() const { | |
57 return this->a() >= 0 && this->a() <= 1 | |
58 && this->r() >= 0 && this->r() <= this->a() | |
59 && this->g() >= 0 && this->g() <= this->a() | |
60 && this->b() >= 0 && this->b() <= this->a(); | |
61 } | |
62 | |
63 private: | |
64 typedef Sk4f INHERITED; | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 #endif//SkPM_DEFINED | |
OLD | NEW |