Code

r10817@tres: ted | 2006-01-29 15:45:22 -0800
[inkscape.git] / src / extension / dxf2svg / aci2rgb.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <string>
6 char* RGB(double R, double G, double B);
7 char* RGB(double R, double G, double B){
8         int r = int (R);
9         int g = int (G);
10         int b = int (B);
11         
12         char out[6];
13         char *chr_ptr;
14         string output;
15         stringstream oss;
16         
17         if (r < 16 ){
18                 oss << 0;
19         }       
20         oss << hex << r;
21         
22         if (g < 16 ){
23                 oss << 0;
24         }
25         oss << hex << g;
26         
27         if (b < 16 ){
28                 oss << 0;
29         }
30         oss << hex << b;
31         
32         output = oss.str();
33         
34         for (int i = 0; i < 6; i++){
35                 out[i] = output[i];
36         }
37         chr_ptr = &out;
38         return chr_ptr;
39 }
40         
42 float aci_to_rgb(int aci);
44 float aci_to_rgb(int aci)
45  {
46         aci = abs(aci);                 // hidden layers have negative color values
47         if (aci<10 || aci>249)  // values of these ranges are special colors
48         {
49                 switch (aci)
50                 {
51                         case 1: return RGB(255,0,0);            // basic colors
52                         case 2: return RGB(255,255,0);
53                         case 3: return RGB(0,255,0);
54                         case 4: return RGB(0,255,255);
55                         case 5: return RGB(0,0,255);
56                         case 6: return RGB(255,0,255);
57                         case 7: return RGB(255,255,255);
58                         case 8: return RGB(128,128,128);
59                         case 9: return RGB(192,192,192);
60                         case 250: return RGB(51,51,51);         // grey shades
61                         case 251: return RGB(91,91,91);
62                         case 252: return RGB(132,132,132);
63                         case 253: return RGB(173,173,173);
64                         case 254: return RGB(214,214,214);
65                         case 255: return RGB(255,255,255);
66                         case 256:                                                       // "by layer"
67                         // Here you should decide how to handle "by layer" logical color.
68                         // Maybe it is a good idea to return a value like -1.
69                         // The outer code will find what is the color of the layer which
70                         // this entity belongs to.
71                                 return -1;
72                 }
73         }
74         // for all the rest of ACI codes
75         float H,S,L,    R,G,B;
76         int remainder = aci % 10;                                       
77         H = 1.5f * (aci - remainder - 10);      // hue in range 0-360
78         S = ((aci % 2) ? 0.5f : 1.0f);          // odd colors have 50% of saturation, even - 100%
79         // set lighteness, the last digit of aci code stands for this
80         if (reminder == 0 || reminder == 1) L = 1.0f; 
81         if (reminder == 2 || reminder == 3) L = 0.8f;
82         if (reminder == 4 || reminder == 5) L = 0.6f;
83         if (reminder == 6 || reminder == 7) L = 0.5f;
84         if (reminder == 8 || reminder == 9) L = 0.3f;
85         // here we have H,S,L set already
86         // let's convert it to RGB, first without consideration of S and L
87         if (H<=120)
88         {
89                 R = (120-H)/60;
90                 G = H/60;
91                 B = 0;
92         }
93         if (H>120 && H<=240)
94         {
95                 R = 0;
96                 G = (240-H)/60;
97                 B = (H-120)/60;
98         }
99         if (H>240 && H<=360)
100         {
101                 R = (H-240)/60;
102                 G = 0;
103                 B = (360-H)/60;
104         }
105         R = min(R, 1);
106         G = min(G, 1);
107         B = min(B, 1);
108         // influence of S and L
109         float max_value = max(R,max(G,B));
110         R = (max_value-S*(max_value-R)) * L * 255;
111         G = (max_value-S*(max_value-G)) * L * 255;
112         B = (max_value-S*(max_value-B)) * L * 255;
113         return RGB(R,G,B);
114  }