home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / color.cxx < prev    next >
C/C++ Source or Header  |  1995-04-06  |  5KB  |  238 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31.  
  32. #if defined(__GNUC__)
  33. #pragma implementation
  34. #endif
  35.  
  36.  
  37.  
  38. #include "ui/color.h"
  39.  
  40. #if defined(__MS_WINDOWS__)
  41. #include <windows.h>
  42.  
  43. #elif defined (__X_MOTIF__)
  44. #include <X11/Xlib.h>
  45.  
  46. #endif
  47.  
  48.  
  49.  
  50. struct ColorStruct {
  51.     double red, green, blue;
  52.     ColorStruct (double r, double g, double b) : red(r), green(g), blue(b){};
  53. };
  54.  
  55.  
  56.  
  57. static ColorStruct Colors (UI_ColorScheme c)
  58. {
  59.     switch(c) {
  60.     case UIColor_Black:
  61.         return ColorStruct (0, 0, 0);
  62.  
  63.     case UIColor_Red:
  64.         return ColorStruct (1, 0, 0);
  65.  
  66.     case UIColor_Green:
  67.         return ColorStruct (0, 1, 0);
  68.  
  69.     case UIColor_Blue:
  70.         return ColorStruct (0, 0, 1);
  71.  
  72.     case UIColor_White:
  73.         return ColorStruct (1, 1, 1);
  74.  
  75.     case UIColor_MediumGray:
  76.         return ColorStruct (0.5, 0.5, 0.5);
  77.  
  78.     default:
  79.         return ColorStruct (0, 0, 0);
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86. UI_Color::UI_Color ()
  87. {
  88.     _red = _green = _blue = 0;
  89.     _colorVal = getColorVal (0, 0, 0);
  90. }
  91.  
  92.  
  93. UI_Color::UI_Color (UI_ColorScheme c)
  94. {
  95.     _colorVal  = getAbsVal (c);
  96.     ColorStruct s = Colors (c);
  97.     _red   = s.red;
  98.     _blue  = s.blue;
  99.     _green = s.green;
  100. }
  101.  
  102.  
  103. UI_Color::UI_Color(double r, double g, double b)
  104. {
  105.     if (r < 0 || r > 1) {
  106.         CL_Error::Warning ("UI_Color constructor: invalid red component "
  107.                            "%e", r);
  108.         _red = _green = _blue = 0;
  109.     }
  110.     else if (g < 0 || g > 1) {
  111.         CL_Error::Warning ("UI_Color constructor: invalid green component "
  112.                            "%e", g);
  113.         _red = _green = _blue = 0;
  114.     }
  115.     else if (b < 0 || b > 1) {
  116.         CL_Error::Warning ("UI_Color constructor: invalid blue component "
  117.                            "%e", b);
  118.         _red = _green = _blue = 0;
  119.     }
  120.     else {
  121.         _red      = r;
  122.         _green    = g;
  123.         _blue     = b;
  124.     }
  125.     _colorVal = getColorVal (_red, _green, _blue);
  126. }
  127.  
  128.  
  129. UI_Color::UI_Color (const UI_Color& c)
  130. {
  131.     _red      = c._red;
  132.     _green    = c._green;
  133.     _blue     = c._blue;
  134.     _colorVal = c._colorVal;
  135. }
  136.  
  137.  
  138. void UI_Color::Red (double r)
  139. {
  140.     if (!PrepareToChange ())
  141.         return;
  142.     _red      = r;
  143.     _colorVal = getColorVal (_red, _green, _blue);
  144.     Notify ();
  145. }
  146.  
  147.  
  148. void UI_Color::Green (double g)
  149. {
  150.     if (!PrepareToChange())
  151.         return;
  152.     _green    = g;
  153.     _colorVal = getColorVal (_red, _green, _blue);
  154.     Notify();
  155. }
  156.  
  157.  
  158. void UI_Color::Blue (double b)
  159. {
  160.     if (!PrepareToChange())
  161.         return;
  162.     _blue     = b;
  163.     _colorVal = getColorVal (_red, _green, _blue);
  164.     Notify();
  165. }
  166.  
  167.  
  168. void UI_Color::Set (UI_ColorScheme c)
  169. {
  170.     if (!PrepareToChange())
  171.         return;
  172.  
  173.     _colorVal = getAbsVal (c);
  174.     Notify();
  175. }
  176.  
  177.  
  178. void UI_Color::operator= (const UI_Color& x)
  179. {
  180.     if (!PrepareToChange())
  181.         return;
  182.  
  183.     _red       = x._red;
  184.     _green     = x._green;
  185.     _blue      = x._blue;
  186.     _colorVal  = x._colorVal;
  187.     Notify();
  188. }
  189.  
  190.  
  191. bool UI_Color::operator== (const UI_Color& o) const
  192. {
  193. #if defined(__MS_WINDOWS__) || defined(__OS2__)
  194.     return _colorVal == o._colorVal;
  195.  
  196. #elif defined (__X_MOTIF__)
  197.     bool retval = (_colorVal.red   == o._colorVal.red && 
  198.                    _colorVal.green == o._colorVal.green &&
  199.                    _colorVal.blue  == o._colorVal.blue ? TRUE : FALSE);
  200.     return retval;
  201. #endif
  202. }
  203.  
  204.  
  205. UI_NativeColorRep& UI_Color::NativeForm () const
  206. {
  207.     return (UI_NativeColorRep &) _colorVal;
  208. }
  209.  
  210.  
  211. UI_NativeColorRep UI_Color::getColorVal (double r, double g, double b)
  212. {
  213. #if defined(__MS_WINDOWS__)
  214.  
  215.     return RGB (255 * r, 255 * g, 255 * b); 
  216.  
  217. #elif defined(__OS2__)
  218.     return ((ushort) (255*r) << 16) + ((ushort) (255*g) << 8) +
  219.             ((ushort) (255*b));
  220.             
  221. #elif defined(__X_MOTIF__)
  222.     _colorVal.red   = (short) (UI_MAXCOLORS * r);
  223.     _colorVal.green = (short) (UI_MAXCOLORS * g);
  224.     _colorVal.blue  = (short) (UI_MAXCOLORS * b);
  225.  
  226.     return _colorVal;
  227. #endif
  228.  
  229. }
  230.  
  231.  
  232. UI_NativeColorRep UI_Color::getAbsVal(UI_ColorScheme c)
  233. {
  234.     ColorStruct s = Colors (c);
  235.     return getColorVal (s.red, s.green, s.blue);
  236. }
  237.  
  238.