opennurbs_color.cpp
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018 
00019 const ON_Color ON_Color::UnsetColor(ON_UNSET_COLOR);
00020 
00021 ON_Color::ON_Color() : m_color(0) 
00022 {}
00023 
00024 ON_Color::ON_Color(unsigned int colorref) : m_color(colorref) 
00025 {}
00026 
00027 ON_Color::ON_Color(int r, int g, int b) : m_color(0) 
00028 {
00029   SetRGB(r,g,b);
00030 }
00031 
00032 ON_Color::ON_Color(int r, int g, int b, int a) : m_color(0) 
00033 {
00034   SetRGBA(r,g,b,a);
00035 }
00036 
00037 unsigned int ON_Color::WindowsRGB() const
00038 {
00039   unsigned int RGB = ON_Color(Red(),Green(),Blue());
00040   return RGB;
00041 }
00042 
00043 ON_Color::operator unsigned int() const
00044 {
00045   return m_color;
00046 }
00047 
00048 int ON_Color::Compare( const ON_Color& b ) const
00049 {
00050   unsigned int bc = b;
00051   return (((int)m_color) - ((int)bc));
00052 }
00053 
00054 int ON_Color::Red() const
00055 { return m_color & 0xFF;}
00056 
00057 int ON_Color::Green() const
00058 { return (m_color>>8) & 0xFF;}
00059 
00060 int ON_Color::Blue() const
00061 { return (m_color>>16) & 0xFF;}
00062 
00063 int ON_Color::Alpha() const
00064 { return (m_color>>24) & 0xFF;}
00065 
00066 double ON_Color::FractionRed() const
00067 { 
00068   //return Red()/255.0;
00069   return (m_color & 0xFF)*0.003921568627450980392156862745; // better fodder for optimizer
00070 }
00071 
00072 double ON_Color::FractionGreen() const       
00073 { 
00074   //return Green()/255.0;
00075   return ((m_color>>8) & 0xFF)*0.003921568627450980392156862745; // better fodder for optimizer
00076 }
00077 
00078 double ON_Color::FractionBlue() const       
00079 { 
00080   //return Blue()/255.0;
00081   return ((m_color>>16) & 0xFF)*0.003921568627450980392156862745; // better fodder for optimizer
00082 }
00083 
00084 double ON_Color::FractionAlpha() const       
00085 { 
00086   //return Alpha()/255.0;
00087   return ((m_color>>24) & 0xFF)*0.003921568627450980392156862745; // better fodder for optimizer
00088 }
00089 
00090 void ON_Color::SetRGB(int r,int g,int b) // 0 to 255
00091 {
00092   SetRGBA(r,g,b,0);
00093 }
00094 
00095 void ON_Color::SetFractionalRGB(double r,double g,double b)
00096 {
00097   SetFractionalRGBA(r,g,b,0.0);
00098 }
00099 
00100 void ON_Color::SetAlpha(int alpha)
00101 {
00102         if (alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255; 
00103         m_color = (m_color & 0x00FFFFFF) | (alpha << 24 );
00104 }
00105 
00106 void ON_Color::SetFractionalAlpha(double alpha)
00107 {
00108         if (alpha < 0.0 ) alpha = 0.0; else if ( alpha > 1.0 ) alpha = 1.0;     
00109   SetAlpha((int)(alpha*255.0));
00110 }
00111 
00112 void
00113 ON_Color::SetRGBA( int red, int green, int blue, int alpha )
00114 {
00115         if (red   < 0 ) red   = 0; else if ( red   > 255 ) red   = 255; 
00116         if (green < 0 ) green = 0; else if ( green > 255 ) green = 255; 
00117         if (blue  < 0 ) blue  = 0; else if ( blue  > 255 ) blue  = 255; 
00118         if (alpha < 0 ) alpha = 0; else if ( alpha > 255 ) alpha = 255; 
00119         m_color = (alpha << 24 ) | (blue << 16) | (green << 8) | red;
00120 }
00121 
00122 void
00123 ON_Color::SetFractionalRGBA( double red, double green, double blue, double alpha )
00124 {
00125   int r,g,b,a;
00126         if (red   < 0.0 ) red   = 0.0; else if ( red   > 1.0 ) red   = 1.0;     
00127         if (green < 0.0 ) green = 0.0; else if ( green > 1.0 ) green = 1.0;     
00128         if (blue  < 0.0 ) blue  = 0.0; else if ( blue  > 1.0 ) blue  = 1.0;     
00129         if (alpha < 0.0 ) alpha = 0.0; else if ( alpha > 1.0 ) alpha = 1.0;
00130 
00131   red   *= 255.0;
00132   green *= 255.0;
00133   blue  *= 255.0;
00134   alpha *= 255.0;
00135 
00136   r = (int)red;
00137   g = (int)green;
00138   b = (int)blue;
00139   a = (int)alpha;
00140 
00141   // round to closest int
00142   if( (red-r)>=0.5 ) r++;
00143   if( (green-g)>=0.5 ) g++;
00144   if( (blue-b)>=0.5 ) b++;
00145   if( (alpha-a)>=0.5 ) a++;
00146 
00147   SetRGBA( r, g, b, a );
00148 }
00149 
00150 double ON_Color::Hue() const
00151 {
00152   // returns 0 to 2*pi 
00153   // 0    = red,  pi/3   = yellow, 2*pi/3 = green, 
00154   // pi   = cyan, 4*pi/3 = blue,   5*pi/3 = magenta,
00155   // 2*pi = red
00156   double h;
00157   int r = Red();
00158   int g = Green();
00159   int b = Blue();
00160   int minrgb, maxrgb;
00161   if ( r <= g ) {minrgb = r; maxrgb = g;} else {minrgb = g; maxrgb = r;}
00162   if (minrgb > b) minrgb = b; else if (maxrgb < b ) maxrgb = b;
00163   if ( maxrgb != minrgb ) {
00164     double d = 1.0/(maxrgb - minrgb);
00165     if ( r == maxrgb) {
00166       h = (g - b)*d;
00167       if ( h < 0.0 )
00168         h += 6.0;
00169     }
00170     else if ( g == maxrgb)
00171       h = 2.0 + (b - r)*d;
00172     else 
00173       h = 4.0 + (r - g)*d;
00174     h *= ON_PI/3.0;
00175   }
00176   else
00177     h = 0.0;
00178   return h;
00179 }
00180 
00181 double ON_Color::Saturation() const
00182 {
00183   // 0.0 to 1.0    0.0 = gray,  1.0 = saturated
00184   double s;
00185   int r = Red();
00186   int g = Green();
00187   int b = Blue();
00188   int minrgb, maxrgb;
00189   if ( r <= g ) {minrgb = r; maxrgb = g;} else {minrgb = g; maxrgb = r;}
00190   if (minrgb > b) minrgb = b; else if (maxrgb < b ) maxrgb = b;
00191   if ( maxrgb > 0 ) {
00192     s = ((double)(maxrgb - minrgb))/((double)maxrgb);
00193   }
00194   else
00195     s = 0.0;
00196   return s;
00197 }
00198 
00199 double ON_Color::Value() const
00200 {
00201   // 0.0 to 1.0    0.0 = black, 1.0 = white
00202   int r = Red();
00203   int g = Green();
00204   int b = Blue();
00205   int maxrgb = ( r <= g ) ? g : r; if ( maxrgb < b ) maxrgb = b;
00206   return (maxrgb/255.0);
00207 }
00208 
00209 void ON_Color::SetHSV( 
00210        double hue,         // hue in radians 
00211        double saturation, // satuation 0.0 = gray, 1.0 = saturated
00212        double value       // value     
00213        )
00214 {
00215   int i;
00216   double f, p, q, t, r, g, b;
00217   if ( saturation <= 1.0/256.0 ) {
00218     r = value;
00219     g = value;
00220     b = value;
00221   }
00222   else  {
00223     hue *= 3.0 / ON_PI;  // (6.0 / 2.0 * ON_PI);
00224     i = (int)floor(hue);
00225     if ( i < 0 || i > 5 ) {
00226       hue = fmod(hue,6.0);
00227       if ( hue < 0.0 )
00228         hue += 6.0;
00229       i = (int)floor(hue);
00230     }    
00231     f = hue - i;    
00232     p = value * ( 1.0 - saturation);
00233     q = value * ( 1.0 - ( saturation * f) );
00234     t = value * ( 1.0 - ( saturation * ( 1.0 - f) ) );
00235     switch( i)
00236     {
00237     case 0:
00238       r = value; g = t; b = p; break;      
00239     case 1:
00240       r = q; g = value; b = p; break;
00241     case 2:
00242       r = p; g = value; b = t; break;
00243     case 3:
00244       r = p; g = q; b = value; break;
00245     case 4:
00246       r = t; g = p; b = value; break;
00247     case 5:
00248       r = value; g = p; b = q; break;      
00249     default:
00250       r = 0; g = 0; b = 0; break; // to keep lint quiet
00251     }    
00252   }
00253   SetFractionalRGB(r,g,b);
00254 }
00255 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:27:00