OSDN Git Service

revises makefile
[qcad/qcad.git] / qcadwin / QDrawHSVGraph.cpp
1 //---------------------------------------------------------------------------\r
2 #include <vcl.h>\r
3 #pragma hdrstop\r
4 \r
5 #include <math.h>\r
6 #include "QDrawHSVGraph.h"\r
7 \r
8 //---------------------------------------------------------------------------\r
9 #pragma package(smart_init)\r
10 //---------------------------------------------------------------------------\r
11 /**\r
12  * Constructor\r
13 **/\r
14 QDrawHSVGraph::QDrawHSVGraph(TCanvas *_canvas) {\r
15   Canvas = _canvas;\r
16   ColumnNumber = 16;\r
17   LeftMargin = 150;\r
18   SetSize(32);\r
19 }\r
20 //---------------------------------------------------------------------------\r
21 void QDrawHSVGraph::SetSize(int BoxSize) {\r
22   BoxWidth = BoxSize;\r
23   BoxDepth = int(0.4 * BoxWidth);\r
24   BoxMaxHeight = 2 * BoxWidth;\r
25   BoxOffset = (0.2 * BoxWidth);\r
26 \r
27   TopMargin = BoxMaxHeight + 10;\r
28 }\r
29 \r
30 //---------------------------------------------------------------------------\r
31 /**\r
32  * Draw a box\r
33 **/\r
34 void QDrawHSVGraph::DrawBox(int _x, int _y, int height, double theta) {\r
35   TColor c1 = GetColor(theta, 1.0);\r
36   TColor c2 = GetColor(theta, 0.8);\r
37   TColor c3 = GetColor(theta, 0.6);\r
38 \r
39   SetPenColor(clDkGray);\r
40   //Draw rightside surface\r
41   Canvas->MoveTo(_x + BoxWidth            , _y);\r
42   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y + BoxDepth);\r
43   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y + BoxDepth - height);\r
44   Canvas->LineTo(_x + BoxWidth            , _y            - height);\r
45   Canvas->LineTo(_x + BoxWidth            , _y);\r
46   SetBrushColor(c2);\r
47   Canvas->FloodFill(_x + BoxWidth - 1, _y - 1, clDkGray, fsBorder);\r
48   SetPenColor(clBlack);\r
49   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y + BoxDepth);\r
50   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y + BoxDepth - height);\r
51   Canvas->LineTo(_x + BoxWidth            , _y            - height);\r
52   Canvas->LineTo(_x + BoxWidth            , _y);\r
53 \r
54   //Draw front surface\r
55   SetBrushColor(c1);\r
56   TRect rc(_x - BoxOffset,                _y + BoxDepth - height,\r
57            _x - BoxOffset + BoxWidth + 1, _y + BoxDepth + 1);\r
58   Canvas->FillRect(rc);\r
59   SetBrushColor(clBlack);\r
60   Canvas->FrameRect(rc);\r
61 \r
62   //Draw top surface\r
63   SetPenColor(clDkGray);\r
64   Canvas->MoveTo(_x                       , _y - height);\r
65   Canvas->LineTo(_x + BoxWidth            , _y - height);\r
66   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y - height + BoxDepth);\r
67   Canvas->LineTo(_x            - BoxOffset, _y - height + BoxDepth);\r
68   Canvas->LineTo(_x                       , _y - height);\r
69   SetBrushColor(c3);\r
70   Canvas->FloodFill(_x + 1, _y - height + 1, clDkGray, fsBorder);\r
71   SetPenColor(clBlack);\r
72   Canvas->LineTo(_x + BoxWidth            , _y - height);\r
73   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y - height + BoxDepth);\r
74   Canvas->LineTo(_x            - BoxOffset, _y - height + BoxDepth);\r
75   Canvas->LineTo(_x                       , _y - height);\r
76 \r
77 }\r
78 //---------------------------------------------------------------------------\r
79 void QDrawHSVGraph::DrawBoxBase(int _x, int _y) {\r
80   TColor cl = clLtGray;\r
81 \r
82   SetPenColor(clBlack);\r
83   Canvas->MoveTo(_x                       , _y);\r
84   Canvas->LineTo(_x + BoxWidth            , _y);\r
85   Canvas->LineTo(_x + BoxWidth - BoxOffset, _y + BoxDepth);\r
86   Canvas->LineTo(_x            - BoxOffset, _y + BoxDepth);\r
87   Canvas->LineTo(_x, _y);\r
88   SetBrushColor(cl);\r
89   Canvas->FloodFill(_x + 1, _y + 1, clBlack, fsBorder);\r
90 }\r
91 \r
92 //---------------------------------------------------------------------------\r
93 TColor QDrawHSVGraph::GetColor(double theta, double abVal) {\r
94   double value = theta*56;\r
95   double h,s,v;\r
96 \r
97   h = value;\r
98   s = abVal;\r
99   v = 1;\r
100 \r
101   float r = 0;\r
102   float g = 0;\r
103   float b = 0;\r
104   if (s < 0) s = 0;\r
105   if (s > 1) s = 1;\r
106   if (v < 0) v = 0;\r
107   if (v > 1) v = 1;\r
108 \r
109   h = h / 60;\r
110   int i = int(h);\r
111   float f = h - i;\r
112   float p1 = v * (1 - s);\r
113   float p2 = v * (1 - s*f);\r
114   float p3 = v * (1 - s*(1 - f));\r
115 if (i == 0) { r = v; g = p3; b = p1; }\r
116   if (i == 1) { r = p2;g = v;  b = p1; }\r
117   if (i == 2) { r = p1;g = v;  b = p3; }\r
118   if (i == 3) { r = p1;g = p2; b = v;  }\r
119   if (i == 4) { r = p3;g = p1; b = v;  }\r
120   if (i == 5) { r = v; g = p1; b = p2; }\r
121 \r
122   int R = (int)(r*255);\r
123   int G = (int)(g*255);\r
124   int B = (int)(b*255);\r
125 \r
126   TColor Color = TColor(0x10000*R+0x100*G+B);\r
127   return Color;\r
128 }\r
129 //------------------------------------------------------------------------\r
130 void QDrawHSVGraph::DrawTest(void) {\r
131 //DrawBox(100, 100, 50, 0);\r
132   DrawBoxBase(100, 100);\r
133 }\r
134 //------------------------------------------------------------------------\r
135 void QDrawHSVGraph::DrawAll(QBits * qBits) {\r
136 // clear canvas\r
137   SetBrushColor(clWhite);\r
138   Canvas->FloodFill(1, 1, clDkGray, fsBorder);\r
139 \r
140   const int N = qBits->GetNumberOfStates();\r
141   const double eps = 1.0e-20;\r
142 \r
143   for (int i = 0; i < N; i++) {\r
144     double im = qBits->NthStateI(i);\r
145     double re = qBits->NthStateR(i);\r
146     double R  = sqrt(im * im + re * re);\r
147     double theta = 3.1416 + atan2(im, re + eps);\r
148 \r
149     int ix = i % ColumnNumber;\r
150     int iy = (i - ix) / ColumnNumber;\r
151     int x  = LeftMargin + ix * BoxWidth - iy * BoxOffset;\r
152     int y  = TopMargin  + iy * BoxDepth;\r
153 \r
154     if (R == 0) {\r
155       DrawBoxBase(x, y);\r
156     } else {\r
157       DrawBox(x, y, int(R * BoxMaxHeight), theta);\r
158     }\r
159   }\r
160 \r
161 // draw HSV sumple\r
162   for (int i = 0; i < 208; i++) {\r
163     SetPenColor(GetColor(0.03 * i, 1.0));\r
164     Canvas->MoveTo(10, TopMargin + i);\r
165     Canvas->LineTo(10 + BoxDepth, TopMargin + i);\r
166   }\r
167 }\r
168 //-----------------------------------------------------------------------\r
169 void QDrawHSVGraph::DrawAll2D(QBits * qBits) {\r
170 // clear canvas\r
171   SetBrushColor(clWhite);\r
172   Canvas->FloodFill(1, 1, clDkGray, fsBorder);\r
173 \r
174   const int N = qBits->GetNumberOfStates();\r
175   const double eps = 1.0e-20;\r
176 \r
177   for (int i = 0; i < N; i++) {\r
178     double im = qBits->NthStateI(i);\r
179     double re = qBits->NthStateR(i);\r
180     double R  = sqrt(im * im + re * re);\r
181     double theta = 3.1416 + atan2(im, re + eps);\r
182 \r
183     int ix = i % ColumnNumber;\r
184     int iy = (i - ix) / ColumnNumber;\r
185     int x  = 50 + ix * BoxWidth;\r
186     int y  = 20 + iy * BoxWidth;\r
187 \r
188     TRect rc(x, y, x + BoxWidth + 1, y + BoxWidth + 1);\r
189     SetBrushColor(GetColor(theta, R));\r
190     Canvas->FillRect(rc);\r
191     SetBrushColor(clBlack);\r
192     Canvas->FrameRect(rc);\r
193   }\r
194 \r
195 // draw HSV sumple\r
196   for (int i = 0; i < 208; i++) {\r
197     SetPenColor(GetColor(0.03 * i, 1.0));\r
198     Canvas->MoveTo(10, TopMargin + i);\r
199     Canvas->LineTo(10 + BoxDepth, TopMargin + i);\r
200   }\r
201 \r
202 }\r
203 \r