1 /* 2 Copyright 2010 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #ifndef GrColor_DEFINED 19 #define GrColor_DEFINED 20 21 #include "GrTypes.h" 22 23 /** 24 * GrColor is 4 bytes for R, G, B, A, in a compile-time specific order. The 25 * components are stored premultiplied. 26 */ 27 typedef uint32_t GrColor; 28 29 // indices for address a GrColor as an array of bytes 30 31 #define GrColor_INDEX_R 0 32 #define GrColor_INDEX_G 1 33 #define GrColor_INDEX_B 2 34 #define GrColor_INDEX_A 3 35 36 // shfit amount to assign a component to a GrColor int 37 38 #define GrColor_SHIFT_R 0 39 #define GrColor_SHIFT_G 8 40 #define GrColor_SHIFT_B 16 41 #define GrColor_SHIFT_A 24 42 43 /** 44 * Pack 4 components (RGBA) into a GrColor int 45 */ 46 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, 47 unsigned b, unsigned a) { 48 GrAssert((uint8_t)r == r); 49 GrAssert((uint8_t)g == g); 50 GrAssert((uint8_t)b == b); 51 GrAssert((uint8_t)a == a); 52 return (r << GrColor_SHIFT_R) | 53 (g << GrColor_SHIFT_G) | 54 (b << GrColor_SHIFT_B) | 55 (a << GrColor_SHIFT_A); 56 } 57 58 // extract a component (byte) from a GrColor int 59 60 #define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF) 61 #define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF) 62 #define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF) 63 #define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF) 64 65 /** 66 * Since premultiplied means that alpha >= color, we construct a color with 67 * each component==255 and alpha == 0 to be "illegal" 68 */ 69 #define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A)) 70 71 #endif 72 73