1 | /* $Id$Revision: */ |
2 | /* vim:set shiftwidth=4 ts=8: */ |
3 | |
4 | /************************************************************************* |
5 | * Copyright (c) 2011 AT&T Intellectual Property |
6 | * All rights reserved. This program and the accompanying materials |
7 | * are made available under the terms of the Eclipse Public License v1.0 |
8 | * which accompanies this distribution, and is available at |
9 | * http://www.eclipse.org/legal/epl-v10.html |
10 | * |
11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
12 | *************************************************************************/ |
13 | |
14 | #include "general.h" |
15 | #include "colorutil.h" |
16 | |
17 | static void r2hex(float r, char *h){ |
18 | /* convert a number in [0,1] to 0 to 255 then to a hex */ |
19 | static char hex[] = "0123456789abcdef" ; |
20 | int i = (int)(255*r+0.5); |
21 | int j = i%16; |
22 | int k = i/16; |
23 | h[0] = hex[k]; |
24 | h[1] = hex[j]; |
25 | } |
26 | |
27 | void rgb2hex(float r, float g, float b, char *cstring, char *opacity){ |
28 | cstring[0] = '#'; |
29 | r2hex(r, &(cstring[1])); |
30 | r2hex(g, &(cstring[3])); |
31 | r2hex(b, &(cstring[5])); |
32 | //set to semitransparent for multiple sets vis |
33 | if (opacity && strlen(opacity) >= 2){ |
34 | cstring[7] = opacity[0]; |
35 | cstring[8] = opacity[1]; |
36 | cstring[9]='\0'; |
37 | } else { |
38 | cstring[7] = '\0'; |
39 | } |
40 | } |
41 | |
42 | real Hue2RGB(real v1, real v2, real H) { |
43 | if(H < 0.0) H += 1.0; |
44 | if(H > 1.0) H -= 1.0; |
45 | if((6.0*H) < 1.0) return (v1 + (v2 - v1) * 6.0 * H); |
46 | if((2.0*H) < 1.0) return v2; |
47 | if((3.0*H) < 2.0) return (v1 + (v2 - v1) * ((2.0/3.0) - H) * 6.0); |
48 | return v1; |
49 | } |
50 | |
51 | char *hex[16]={"0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9" ,"a" ,"b" ,"c" ,"d" ,"e" ,"f" }; |
52 | |
53 | char * hue2rgb(real hue, char *color){ |
54 | real v1, v2, lightness = .5, saturation = 1; |
55 | int red, blue, green; |
56 | |
57 | if(lightness < 0.5) |
58 | v2 = lightness * (1.0 + saturation); |
59 | else |
60 | v2 = (lightness + saturation) - (saturation * lightness); |
61 | |
62 | v1 = 2.0 * lightness - v2; |
63 | |
64 | red = (int)(255.0 * Hue2RGB(v1, v2, hue + (1.0/3.0)) + 0.5); |
65 | green = (int)(255.0 * Hue2RGB(v1, v2, hue) + 0.5); |
66 | blue = (int)(255.0 * Hue2RGB(v1, v2, hue - (1.0/3.0)) + 0.5); |
67 | color[0] = '#'; |
68 | sprintf(color+1,"%s" ,hex[red/16]); |
69 | sprintf(color+2,"%s" ,hex[red%16]); |
70 | sprintf(color+3,"%s" ,hex[green/16]); |
71 | sprintf(color+4,"%s" ,hex[green%16]); |
72 | sprintf(color+5,"%s" ,hex[blue/16]); |
73 | sprintf(color+6,"%s" ,hex[blue%16]); |
74 | color[7] = '\0'; |
75 | return color; |
76 | } |
77 | |
78 | |
79 | void hue2rgb_real(real hue, real *color){ |
80 | real v1, v2, lightness = .5, saturation = 1; |
81 | int red, blue, green; |
82 | |
83 | if(lightness < 0.5) |
84 | v2 = lightness * (1.0 + saturation); |
85 | else |
86 | v2 = (lightness + saturation) - (saturation * lightness); |
87 | |
88 | v1 = 2.0 * lightness - v2; |
89 | |
90 | red = (int)(255.0 * Hue2RGB(v1, v2, hue + (1.0/3.0)) + 0.5); |
91 | green = (int)(255.0 * Hue2RGB(v1, v2, hue) + 0.5); |
92 | blue = (int)(255.0 * Hue2RGB(v1, v2, hue - (1.0/3.0)) + 0.5); |
93 | |
94 | |
95 | color[0] = red/255.; |
96 | color[1] = green/255.; |
97 | color[2] = blue/255.; |
98 | |
99 | } |
100 | |