home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / COLOR.H < prev    next >
C/C++ Source or Header  |  1997-09-20  |  4KB  |  249 lines

  1. //////////////////
  2. // color module //
  3. //////////////////
  4.  
  5. #ifndef __COLOR_H
  6. #define __COLOR_H
  7.  
  8. #include "misc.h"
  9. #include "globals.h"
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. class COLOR
  19. {
  20.     public:
  21.  
  22.         // constructors
  23.         COLOR(float i,int mode=ABSOLUTE);
  24.         COLOR(float c1,float c2,float c3,float c4=0,int mode=ABSOLUTE,int model=RGBA);
  25.  
  26.         // format type
  27.         int type;
  28.         
  29.         // color model
  30.         int model;
  31.  
  32.         // operation mode
  33.         int mode;
  34.  
  35.         // operators
  36.         inline int operator ==(COLOR const &other) const;
  37.         inline int operator !=(COLOR const &other) const;
  38.  
  39.         // color components
  40.         float c1;                   // |R|C|C|H|H|Y|Y| Y|
  41.         float c2;                   // |G|M|M|S|L|I|U|Cb|
  42.         float c3;                   // |G|Y|Y|I|S|Q|V|Cr|
  43.         union 
  44.         {
  45.             float c4;               // |A|A|K|A|A|A|A| A|
  46.             float index;            // color index
  47.         };
  48. };
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. inline int COLOR::operator ==(COLOR const &other) const
  59. {
  60.     // must have matching modes
  61.     if (mode!=other.mode) return 0;
  62.  
  63.     // check for mismatch
  64.     if (type==DIRECT && model==other.model)
  65.     {
  66.         // check direct color components
  67.         if (c1!=other.c1 || c2!=other.c2 || c3!=other.c3 || c4!=other.c4) return 0;   
  68.     }
  69.     else if (type==INDEXED && index!=other.index) return 0;
  70.  
  71.     // equal
  72.     return 1;
  73. }
  74.  
  75.  
  76. inline int COLOR::operator !=(COLOR const &other) const
  77. {
  78.     return !(*this==other);
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. #if defined(__WATCOMC__)
  96.  
  97. // Watcom C++ version
  98.  
  99. // pack (r,g,b) into 32bit color integer
  100. uint extern RGB32(uchar r,uchar g,uchar b);
  101. #pragma aux RGB32 =              \
  102.         "and eax,0000FFFFh"      \
  103.         "shl eax,8"              \
  104.         "mov al,cl"              \
  105.         parm [ah][al][cl];
  106.  
  107. // pack (r,g,b,a) to 32bit color integer
  108. uint extern RGBA32(uchar r,uchar g,uchar b,uchar a);
  109. #pragma aux RGBA32 =             \
  110.         "shl eax,16"             \
  111.         "mov ax,cx"              \
  112.         parm [al][ch][cl][ah];
  113.  
  114.  
  115.  
  116. #elif defined(__VISUALC__)
  117.  
  118. // Visual C++ version
  119.  
  120. // disable 'no return value' messages (return is EAX)
  121. #pragma warning(disable:4035)
  122.  
  123. // pack (r,g,b) into 32bit color integer
  124. inline uint RGB32(uchar r,uchar g,uchar b)
  125. {
  126.     __asm
  127.     {
  128.         mov ah,r
  129.         mov al,g
  130.         mov cl,b
  131.         and eax,0000FFFFh
  132.         shl eax,8
  133.         mov al,cl
  134.     }
  135. }
  136.  
  137. // pack (r,g,b,a) to 32bit color integer
  138. inline uint RGBA32(uchar r,uchar g,uchar b,uchar a)
  139. {
  140.     __asm
  141.     {
  142.         mov al,r
  143.         mov ch,g
  144.         mov cl,b
  145.         mov ah,a
  146.         shl eax,16
  147.         mov ax,cx
  148.     }
  149. }
  150.  
  151. // renable warning
  152. #pragma warning(default:4035)
  153.  
  154.  
  155. #else 
  156.  
  157. // portable c++ version.
  158.  
  159. // pack (r,g,b) into 32bit color integer
  160. inline uint RGB32(uchar r,uchar g,uchar b)
  161. {
  162.     uint color=r<<16;
  163.     color+=g<<8;
  164.     color+=b;
  165.     return color;
  166. }
  167.  
  168. // pack (r,g,b,a) to 32bit color integer
  169. inline uint RGBA32(uchar r,uchar g,uchar b,uchar a)
  170. {
  171.     uint color=a<<24;
  172.     color+=r<<16;
  173.     color+=g<<8;
  174.     color+=b;
  175.     return color;
  176. }
  177.  
  178. #endif
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. // unpack 32bit color into r,g,b,a components
  186. inline void UnpackRGB32(uint color,uchar &r,uchar &g,uchar &b)
  187. {
  188.     uchar *color_uchar=(uchar*)&color;
  189.     r=color_uchar[2];
  190.     g=color_uchar[1];
  191.     b=color_uchar[0];
  192. }
  193.  
  194. // unpack RGBA32 color integer into RGBA components
  195. inline void UnpackRGBA32(uint color,uchar &r,uchar &g,uchar &b,uchar &a)
  196. {
  197.     uchar *color_uchar=(uchar*)&color;
  198.     r=color_uchar[2];
  199.     g=color_uchar[1];
  200.     b=color_uchar[0];
  201.     a=color_uchar[3];
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. // pack r,g,b into 16bit color (RGB565)
  215. inline ushort RGB16(uchar r,uchar g,uchar b)
  216. {
  217.     ushort color=(r&0xF8)<<8;
  218.     color+=(g&0xFC)<<3;
  219.     color+=b>>3;
  220.     return color;
  221. }
  222.  
  223.  
  224. // unpack 16bit color into r,g,b components
  225. inline void UnpackRGB16(ushort color,uchar &r,uchar &g,uchar &b)
  226. {
  227. //    r=color>>11;
  228. //    g=(color>>5) & 0x3F;
  229. //    b=color & 0x1F;
  230.  
  231.     r=(color>>8)&0xF8;
  232.     g=(color>>3)&0xFC;
  233.     b=(color<<3)&0xF8;
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. #endif
  249.