Skip to content
Snippets Groups Projects
Commit 09ad9143 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

better rgb/hsv conversion
parent 1bfe2b6a
No related branches found
No related tags found
No related merge requests found
// $Id: ColorTable.cpp,v 1.31 2006-01-06 00:34:20 geuzaine Exp $ // $Id: ColorTable.cpp,v 1.32 2006-08-25 23:01:16 geuzaine Exp $
// //
// Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
// //
...@@ -424,62 +424,41 @@ int ColorTable_IsAlpha(GmshColorTable * ct) ...@@ -424,62 +424,41 @@ int ColorTable_IsAlpha(GmshColorTable * ct)
// HSV/RBG conversion routines // HSV/RBG conversion routines
#define UNDEFINED 0 void HSV_to_RGB(double H, double S, double V,
double *R, double *G, double *B)
// rgb on [0, 1], sv returned on [0, 1] and h on [0, 6].
// Exception: h is returned UNDEFINED if S==0.
#define RETURN_HSV(h,s,v) {*H=h; *S=s; *V=v; return;}
void RGB_to_HSV(double R, double G, double B,
double *H, double *S, double *V)
{ {
double v, x, f; if(S < 5.0e-6) {
int i; *R = *G = *B = V;
}
x = DMIN(DMIN(R, G), B); else {
v = DMAX(DMAX(R, G), B); int i = (int)H;
if(v == x) double f = H - (float)i;
RETURN_HSV(UNDEFINED, 0, v); double p1 = V*(1.0-S);
f = (R == x) ? G - B : ((G == x) ? B - R : R - G); double p2 = V*(1.0-S*f);
i = (R == x) ? 3 : ((G == x) ? 5 : 1); double p3 = V*(1.0-S*(1.0-f));
RETURN_HSV(i - f / (v - x), (v - x) / v, v); switch(i){
case 0: *R = V; *G = p3; *B = p1; break;
case 1: *R = p2; *G = V; *B = p1; break;
case 2: *R = p1; *G = V; *B = p3; break;
case 3: *R = p1; *G = p2; *B = V; break;
case 4: *R = p3; *G = p1; *B = V; break;
case 5: *R = V; *G = p1; *B = p2; break;
}
}
} }
// h given on [0, 6] or UNDEFINED. s and v given on [0, 1]. void RGB_to_HSV(double R, double G, double B,
// rgb each returned on [0, 1]. double *H, double *S, double *V)
#define RETURN_RGB(r,g,b) {*R=r; *G=g; *B=b; return;}
void HSV_to_RGB(double H, double S, double V,
double *R, double *G, double *B)
{ {
double m, n, f; double maxv = R > G ? R : G; if(B > maxv) maxv = B;
int i; *V = maxv;
if(maxv>0){
if(H == UNDEFINED) double minv = R < G ? R : G; if(B < minv) minv = B;
RETURN_RGB(V, V, V); *S = 1.0 - double(minv)/maxv;
i = (int)floor(H); if(maxv > minv){
f = H - i; if(maxv == R){ *H = (G-B)/double(maxv-minv); if (*H<0) *H += 6.0; }
if(!(i & 1)) else if(maxv == G) *H = 2.0+(B-R)/double(maxv-minv);
f = 1 - f; // if i is even else *H = 4.0+(R-G)/double(maxv-minv);
m = V * (1 - S); }
n = V * (1 - S * f);
switch (i) {
case 6:
case 0:
RETURN_RGB(V, n, m);
case 1:
RETURN_RGB(n, V, m);
case 2:
RETURN_RGB(m, V, n);
case 3:
RETURN_RGB(m, n, V);
case 4:
RETURN_RGB(n, m, V);
case 5:
RETURN_RGB(V, m, n);
} }
} }
$Id: TODO,v 1.15 2006-08-22 17:47:08 geuzaine Exp $ $Id: TODO,v 1.16 2006-08-25 23:01:16 geuzaine Exp $
********************************************************************
add a "Box" tab to the clipping plane dialog to create a clip
box in one step (instead of defining 6 planes)
********************************************************************
extrude along a given function (e.g. radius to extrude a sphere) extrude along a given function (e.g. radius to extrude a sphere)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment