ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gfview/clrutils.cpp
Revision: 45
Committed: Tue Sep 6 18:19:20 2011 UTC (8 years, 7 months ago) by gpertea
File size: 2235 byte(s)
Log Message:
added gfview files

Line File contents
1 /*
2 * clrutils.cpp
3 *
4 * Created on: Sep 16, 2008
5 * Author: gpertea
6 */
7
8 /*================== useful rgb <-> hls utility functions I should put
9 in a separate module
10 */
11 #include "clrutils.h"
12 #include "GBase.h"
13
14 inline double h_value(double n1, double n2, double hue) {
15 if (hue<0) hue += 360;
16 if (hue>360) hue -= 360;
17 if (hue<60) return n1+(n2-n1)*hue/60.;
18 else if (hue<180) return n2;
19 else if (hue<240)
20 return n1+(n2-n1)*(240-hue)/60.;
21 else
22 return n1;
23 }
24
25 FXColor hls2rgb(int &H, int &L, int &S) {
26 double h,l,s;
27 double r,g,b,m1,m2;
28 h = ((double)H)/255. * 360;
29 l = ((double)L)/255.;
30 s = ((double)S)/255.;
31 if (l<.5) m2 = l*(1+s);
32 else m2 = l+s-l*s;
33 m1 = 2*l-m2;
34 if (s==0) {
35 r=l;
36 g=l;
37 b=l;
38 }
39 else {
40 r=h_value(m1,m2,h+120);
41 g=h_value(m1,m2,h);
42 b=h_value(m1,m2,h-120);
43 }
44 return FXRGB((int)(r*255), (int)(g*255), (int)(b*255));
45 }
46
47 void rgb2hls(FXColor color, int &H, int &L, int &S) {
48 double r,g,b;
49 double h,l,s,min,max,delta;
50 r = ((double)FXREDVAL(color)) / 255. ;
51 g = ((double)FXGREENVAL(color)) / 255. ;
52 b = ((double)FXBLUEVAL(color)) / 255. ;
53 min = FXMIN3(r,g,b);
54 max = FXMAX3(r,g,b);
55 l = (max+min)/2;
56 if (max==min) {
57 s = 0;
58 h = 0;
59 }
60 else {
61 if (l<=0.5) s = (max-min)/(max+min);
62 else s = (max-min)/(2-max-min);
63 delta = max-min;
64 if (r==max) h=(g-b)/delta;
65 else if (g==max) h=2+(b-r)/delta;
66 else h=4+(r-g)/delta;
67 h /= 6.;
68 if (h<0) h ++;
69 }
70 H = iround(h*255.0);
71 L = iround(l*255.0);
72 S = iround(s*255.0);
73 }
74
75 //adjust h,l,s values for a color
76 FXColor modhls (FXColor src, int h, int l, int s) {
77 int H,L,S;
78 rgb2hls(src, H, L, S);
79 H+=h;L+=l;S+=s;
80 if (H>255) H %= 255;
81 //H=FXCLAMP(0,H,255);
82 L=FXCLAMP(0,L,255);
83 S=FXCLAMP(0,S,255);
84 return hls2rgb(H,L,S);
85 }
86
87 FXColor getRColor(FXColor base, int range, int i) {
88 int hstep=255/(range>>2);
89 if (hstep>70) hstep=70;
90 if (hstep<10) hstep=35;
91 return modhls(base,hstep*(i+1), (i/(range>>2))*10, (i/(range>>2)*12));
92 }