Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_colors.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2014 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <iostream>
17 #include <math.h>
18 #include "a_colors.h"
19 namespace a_colors
20 {
21  //---------------------------------------------------------------------------
22  double min(double a, double b)
23  {
24  if (a<b)
25  return a;
26  else
27  return b;
28  }
29  //---------------------------------------------------------------------------
30  double max(double a, double b)
31  {
32  return -(a_colors::min(-a,-b));
33  }
34  //---------------------------------------------------------------------------
35  double min(double a, double b, double c)
36  {
37  return min(min(a,b),c);
38  }
39  //---------------------------------------------------------------------------
40  double max(double a, double b, double c)
41  {
42  return max(max(a,b),c);
43  }
44  //---------------------------------------------------------------------------
45  void cmy2rgb(double C, double M, double Y, double& R, double& G, double&B)
46  {
47  R = int(255*(1.-C)+.5);
48  G = int(255*(1.-M)+.5);
49  B = int(255*(1.-Y)+.5);
50  }
51  //---------------------------------------------------------------------------
52  void cmyk2cmy(double C, double M, double Y, double K, double& Cn, double& Mn, double&Yn)
53  {
54  Cn = C*(1.-K);
55  Mn = M*(1.-K);
56  Yn = Y*(1.-K);
57  }
58  //---------------------------------------------------------------------------
59  void rgb2hsv(double R, double G, double B, double& H, double& S, double&V)
60  {
61  double var_R = ( R / 255 ); //RGB values = 0 ÷ 255
62  double var_G = ( G / 255 );
63  double var_B = ( B / 255 );
64 
65  double var_Min = min( var_R, var_G, var_B ); //Min. value of RGB
66  double var_Max = max( var_R, var_G, var_B ); //Max. value of RGB
67  double del_Max = var_Max - var_Min; //Delta RGB value
68 
69  V = var_Max;
70 
71  if ( del_Max == 0 ) //This is a gray, no chroma...
72  {
73  H = 0; //HSV results = 0 ÷ 1
74  S = 0;
75  }
76  else //Chromatic data...
77  {
78  S = del_Max / var_Max;
79 
80  double del_R = ( ( ( var_Max - var_R ) / 6. ) + ( del_Max / 2. ) ) / del_Max;
81  double del_G = ( ( ( var_Max - var_G ) / 6. ) + ( del_Max / 2. ) ) / del_Max;
82  double del_B = ( ( ( var_Max - var_B ) / 6. ) + ( del_Max / 2. ) ) / del_Max;
83 
84  if ( var_R == var_Max ) H = del_B - del_G;
85  else if ( var_G == var_Max ) H = ( 1. / 3. ) + del_R - del_B;
86  else if ( var_B == var_Max ) H = ( 2. / 3. ) + del_G - del_R;
87  if ( H < 0. ) H += 1.;
88  if ( H > 1. ) H -= 1.;
89  }
90  }
91  //---------------------------------------------------------------------------
92  void hsv2rgb(double H, double S, double V, double& R, double& G, double&B)
93  {
94  if (S==0)
95  {
96  S *= 255;
97  R = G = B = fabs(S);
98  return;
99  }
100  if (H==1) H=0;
101  H *= 6;
102  int sector = floor(H);
103  double rl = H-sector;
104  double rr = 1.-rl;
105  double chroma = V*S;
106  switch (sector) {
107  case 0:
108  R = V;
109  G = V-chroma*rr;
110  B = V-chroma;
111  break;
112  case 1:
113  R = V-chroma*rl;
114  G = V;
115  B = V-chroma;
116  break;
117  case 2:
118  R = V-chroma;
119  G = V;
120  B = V-chroma*rr;
121  break;
122  case 3:
123  R = V-chroma;
124  G = V-chroma*rl;
125  B = V;
126  break;
127  case 4:
128  R = V-chroma*rr;
129  G = V-chroma;
130  B = V;
131  break;
132  case 5:
133  R = V;
134  G = V-chroma;
135  B = V-chroma*rl;
136  }
137  R = fabs(R*255);
138  G = fabs(G*255);
139  B = fabs(B*255);
140  }
141  //---------------------------------------------------------------------------
142 }
void rgb2hsv(double R, double G, double B, double &H, double &S, double &V)
Definition: a_colors.cxx:59
void cmyk2cmy(double C, double M, double Y, double K, double &Cn, double &Mn, double &Yn)
Definition: a_colors.cxx:52
void cmy2rgb(double C, double M, double Y, double &R, double &G, double &B)
Definition: a_colors.cxx:45
double min(double a, double b)
Definition: a_colors.cxx:22
void hsv2rgb(double H, double S, double V, double &R, double &G, double &B)
Definition: a_colors.cxx:92
double max(double a, double b)
Definition: a_colors.cxx:30