• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2010 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 
11 #ifndef GrColor_DEFINED
12 #define GrColor_DEFINED
13 
14 #include "GrTypes.h"
15 
16 /**
17  *  GrColor is 4 bytes for R, G, B, A, in a compile-time specific order. The
18  *  components are stored premultiplied.
19  */
20 typedef uint32_t GrColor;
21 
22 
23 // shift amount to assign a component to a GrColor int
24 // These shift values are chosen for compatibility with GL attrib arrays
25 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
26 // we'd have to swizzle in shaders. Note the assumption that the cpu is little
27 // endian.
28 #define GrColor_SHIFT_R     0
29 #define GrColor_SHIFT_G     8
30 #define GrColor_SHIFT_B     16
31 #define GrColor_SHIFT_A     24
32 
33 /**
34  *  Pack 4 components (RGBA) into a GrColor int
35  */
GrColorPackRGBA(unsigned r,unsigned g,unsigned b,unsigned a)36 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g,
37                                       unsigned b, unsigned a) {
38     SkASSERT((uint8_t)r == r);
39     SkASSERT((uint8_t)g == g);
40     SkASSERT((uint8_t)b == b);
41     SkASSERT((uint8_t)a == a);
42     return  (r << GrColor_SHIFT_R) |
43             (g << GrColor_SHIFT_G) |
44             (b << GrColor_SHIFT_B) |
45             (a << GrColor_SHIFT_A);
46 }
47 
48 // extract a component (byte) from a GrColor int
49 
50 #define GrColorUnpackR(color)   (((color) >> GrColor_SHIFT_R) & 0xFF)
51 #define GrColorUnpackG(color)   (((color) >> GrColor_SHIFT_G) & 0xFF)
52 #define GrColorUnpackB(color)   (((color) >> GrColor_SHIFT_B) & 0xFF)
53 #define GrColorUnpackA(color)   (((color) >> GrColor_SHIFT_A) & 0xFF)
54 
55 /**
56  *  Since premultiplied means that alpha >= color, we construct a color with
57  *  each component==255 and alpha == 0 to be "illegal"
58  */
59 #define GrColor_ILLEGAL     (~(0xFF << GrColor_SHIFT_A))
60 
61 /** Converts a GrColor to an rgba array of GrGLfloat */
GrColorToRGBAFloat(GrColor color,float rgba[4])62 static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
63     static const float ONE_OVER_255 = 1.f / 255.f;
64     rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
65     rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
66     rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
67     rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
68 }
69 
70 /**
71  * Flags used for bitfields of color components. They are defined so that the bit order reflects the
72  * GrColor shift order.
73  */
74 enum GrColorComponentFlags {
75     kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
76     kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
77     kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
78     kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
79 
80     kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
81                                   kB_GrColorComponentFlag),
82 
83     kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
84                                    kB_GrColorComponentFlag | kA_GrColorComponentFlag)
85 };
86 
GrColorComponentFlagToChar(GrColorComponentFlags component)87 static inline char GrColorComponentFlagToChar(GrColorComponentFlags component) {
88     SkASSERT(GrIsPow2(component));
89     switch (component) {
90         case kR_GrColorComponentFlag:
91             return 'r';
92         case kG_GrColorComponentFlag:
93             return 'g';
94         case kB_GrColorComponentFlag:
95             return 'b';
96         case kA_GrColorComponentFlag:
97             return 'a';
98         default:
99             GrCrash("Invalid color component flag.");
100             return '\0';
101     }
102 }
103 
GrPixelConfigComponentMask(GrPixelConfig config)104 static inline uint32_t GrPixelConfigComponentMask(GrPixelConfig config) {
105     SkASSERT(config >= 0 && config < kGrPixelConfigCnt);
106     static const uint32_t kFlags[] = {
107         0,                              // kUnknown_GrPixelConfig
108         kA_GrColorComponentFlag,        // kAlpha_8_GrPixelConfig
109         kRGBA_GrColorComponentFlags,    // kIndex_8_GrPixelConfig
110         kRGB_GrColorComponentFlags,     // kRGB_565_GrPixelConfig
111         kRGBA_GrColorComponentFlags,    // kRGBA_4444_GrPixelConfig
112         kRGBA_GrColorComponentFlags,    // kRGBA_8888_GrPixelConfig
113         kRGBA_GrColorComponentFlags,    // kBGRA_8888_GrPixelConfig
114     };
115     return kFlags[config];
116 
117     GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
118     GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
119     GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
120     GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
121     GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
122     GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
123     GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
124     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFlags) == kGrPixelConfigCnt);
125 }
126 
127 #endif
128