Coconut Data Framework  beta
CNColor.h
Go to the documentation of this file.
00001 
00008 #import <Coconut/Coconut.h>
00009 #import <OpenGL/gl.h>
00010 
00012 #define CNColorPartNum          4
00013 
00015 typedef enum {
00016         CNRedPos                = 0,            
00017         CNGreenPos              = 1,            
00018         CNBluePos               = 2,            
00019         CNAlphaPos              = 3                     
00020 } CNColorPartIndex ;
00021 
00022 /* They are defined in CNSharedTables.m */
00023 extern const struct CNColor * CNBlack ;           
00024 extern const struct CNColor     * CNRed ;             
00025 extern const struct CNColor     * CNGreen ;           
00026 extern const struct CNColor     * CNBlue ;            
00027 extern const struct CNColor     * CNYellow ;          
00028 extern const struct CNColor     * CNMagenta ;         
00029 extern const struct CNColor     * CNCyan ;            
00030 extern const struct CNColor     * CNWhite ;           
00031 extern const struct CNColor     * CNForegroundColor ; 
00032 extern const struct CNColor * CNBackgroundColor ; 
00035 typedef enum {
00036         CNBlackId                               = 'k',          
00037         CNRedId                                 = 'r',          
00038         CNGreenId                               = 'g',          
00039         CNBlueId                                = 'b',          
00040         CNYellowId                              = 'y',          
00041         CNMagentaId                             = 'm',          
00042         CNCyanId                                = 'c',          
00043         CNWhiteId                               = 'w',          
00044         CNForegroundColorId             = '*',          
00045         CNBackgroundColorId             = '.'           
00046 } CNBasicColorId ;
00047 
00049 static const CNBasicColorId CNNotBasicColorId   = (CNBasicColorId) 0 ;
00050 
00052 typedef uint16_t                CNColor16 ;
00053 
00055 struct CNColor {
00057     uint64_t        hashValue ;
00059         GLfloat                 vector[CNColorPartNum] ;
00060 } ;
00061 
00067 static inline GLfloat
00068 CNRedPartOfColor(const struct CNColor * src)
00069 {
00070         return (src->vector)[CNRedPos] ;
00071 }
00072 
00078 static inline GLfloat
00079 CNGreenPartOfColor(const struct CNColor * src)
00080 {
00081         return (src->vector)[CNGreenPos] ;
00082 }
00083 
00089 static inline GLfloat
00090 CNBluePartOfColor(const struct CNColor * src)
00091 {
00092         return (src->vector)[CNBluePos] ;
00093 }
00094 
00100 static inline GLfloat
00101 CNAlphaPartOfColor(const struct CNColor * src)
00102 {
00103         return (src->vector)[CNAlphaPos] ;
00104 }
00105 
00111 static inline const GLfloat *
00112 CNVectorInColor(const struct CNColor * src)
00113 {
00114         return src->vector ;
00115 }
00116 
00122 static inline uint64_t
00123 CNHashValueOfColor(const struct CNColor * src)
00124 {
00125     return src->hashValue ;
00126 }
00127 
00136 static inline int
00137 CNCompareColor(const struct CNColor * src0, const struct CNColor * src1)
00138 {
00139         uint64_t hash0 = src0->hashValue ;
00140     uint64_t hash1 = src1->hashValue ;
00141     if(hash0 == hash1){
00142         return memcmp(src0, src1, sizeof(struct CNColor)) ;
00143     } else {
00144                 if(hash0 > hash1)               { return 1 ; }
00145                 else if(hash0 < hash1)  { return -1 ; }
00146                 else                                    { return 0 ; }
00147     }
00148 }
00149 
00156 static inline CNBasicColorId
00157 CNColorToBasicId(const struct CNColor * src)
00158 {
00159         CNBasicColorId          result ;
00160         if(src == CNBlack){
00161                 result = CNBlackId ;
00162         } else if(src == CNRed){
00163                 result = CNRedId ;
00164         } else if(src == CNGreen){
00165                 result = CNGreenId ;
00166         } else if(src == CNBlue){
00167                 result = CNBlueId ;
00168         } else if(src == CNYellow){
00169                 result = CNYellowId ;
00170         } else if(src == CNMagenta){
00171                 result = CNMagentaId ;
00172         } else if(src == CNCyan){
00173                 result = CNCyanId ;
00174         } else if(src == CNWhite){
00175                 result = CNWhiteId ;
00176         } else if(src == CNForegroundColor){
00177                 result = CNForegroundColorId ;
00178         } else if(src == CNBackgroundColor){
00179                 result = CNBackgroundColorId ;
00180         } else {
00181                 result = CNNotBasicColorId ;
00182         }
00183         return result ;
00184 }
00185 
00191 static inline unsigned int
00192 CNColorPartToColor16Part(GLfloat src)
00193 {
00194         unsigned int s = src * 16.0 ;
00195         unsigned int result ;
00196         if(s >= 1){
00197                 result = ((unsigned int) (s - 1.0)) & 0xf ;
00198         } else {
00199                 result = 0 ;
00200         }
00201         return result ;
00202 }
00203 
00209 static inline CNColor16
00210 CNColorToColor16(const struct CNColor * src)
00211 {
00212         unsigned int d0 = CNColorPartToColor16Part(CNRedPartOfColor(src)) ;
00213         unsigned int d1 = CNColorPartToColor16Part(CNGreenPartOfColor(src)) ;
00214         unsigned int d2 = CNColorPartToColor16Part(CNBluePartOfColor(src)) ;
00215         unsigned int d3 = CNColorPartToColor16Part(CNAlphaPartOfColor(src)) ;
00216         return (d0 << 12) | (d1 << 8) | (d2 << 4) | d3 ;
00217 }
00218 
00219 
00220 
00221