YSTest: ystyle.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ystyle.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2010 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/UI/ystyle.h"
29 #include "YSLib/UI/ywindow.h"
30
31 using namespace ystdex;
32
33 YSL_BEGIN
34
35 YSL_BEGIN_NAMESPACE(Drawing)
36
37 bool
38 DrawRectRoundCorner(const Graphics& g, const Point& pt, const Size& s, Color c)
39 {
40 const SPos x1(pt.X + 1), y1(pt.Y + 1), x2(pt.X + s.Width - 1),
41 y2(pt.Y + s.Height - 1);
42
43 if(YB_LIKELY(x1 <= x2 && y1 <= y2))
44 {
45 bool b(DrawVLineSeg(g, x1 - 1, y1, y2, c));
46
47 b |= DrawHLineSeg(g, y2, x1, x2, c);
48 b |= DrawVLineSeg(g, x2, y1, y2, c);
49 b |= DrawHLineSeg(g, y1 - 1, x1, x2, c);
50 if(YB_LIKELY(s.Width > 4 && s.Height > 4))
51 {
52 DrawPoint(g, x1, y1, c);
53 DrawPoint(g, x1, y2 - 1, c);
54 DrawPoint(g, x2 - 1, y2 - 1, c);
55 DrawPoint(g, x2 - 1, y1, c);
56 }
57 return b;
58 }
59 return false;
60 }
61
62
63 void
64 RectDrawArrow(const Graphics& g, const Point& pt, SDst half_size, Rotation rot,
65 Color c)
66 {
67 YAssert(bool(g), "Invalid graphics context found.");
68
69 SDst x(pt.X), y(pt.Y);
70
71 switch(rot)
72 {
73 case RDeg0:
74 {
75 SDst t(pt.Y);
76
77 for(SDst i(0); i < half_size; ++i)
78 DrawVLineSeg(g, x--, y--, t++, c);
79 }
80 break;
81 case RDeg90:
82 {
83 SDst t(pt.X);
84
85 for(SDst i(0); i < half_size; ++i)
86 DrawHLineSeg(g, y++, x--, t++, c);
87 }
88 break;
89 case RDeg180:
90 {
91 SDst t(pt.Y);
92
93 for(SDst i(0); i < half_size; ++i)
94 DrawVLineSeg(g, x++, y--, t++, c);
95 }
96 break;
97 case RDeg270:
98 {
99 SDst t(pt.X);
100
101 for(SDst i(0); i < half_size; ++i)
102 DrawHLineSeg(g, y--, x--, t++, c);
103 }
104 default:
105 break;
106 }
107 }
108
109 void
110 DrawArrow(const Graphics& g, const Rect& r, SDst half_size, Rotation rot,
111 Color c)
112 {
113 SPos x(r.X), y(r.Y);
114
115 switch(rot)
116 {
117 case RDeg0:
118 case RDeg180:
119 x += (rot == RDeg180
120 ? (r.Width - half_size) : (r.Width + half_size)) / 2;
121 y += (r.Height + 1) / 2;
122 break;
123 case RDeg90:
124 case RDeg270:
125 y += (rot == RDeg90
126 ? (r.Height - half_size) : (r.Height + half_size)) / 2;
127 x += (r.Width + 1) / 2;
128 default:
129 break;
130 }
131 RectDrawArrow(g, Point(x, y), half_size, rot, c);
132 }
133
134 void
135 DrawCross(const Graphics& g, const Point& pt, const Size& s, Color c)
136 {
137 if(YB_LIKELY(s.Width > 8 && s.Height > 8))
138 {
139 const SPos xmin(pt.X + 4), xmax(xmin + s.Width - 8),
140 ymin(pt.Y + 4), ymax(ymin + s.Height - 8);
141
142 DrawLineSeg(g, xmin, ymin, xmax, ymax, c),
143 DrawLineSeg(g, xmax - 1, ymin, xmin - 1, ymax, c);
144 }
145 }
146
147
148 hsl_t
149 ColorToHSL(Color c)
150 {
151 typedef float mid_t; //中间类型。
152
153 const u8 r(c.GetR()), g(c.GetG()), b(c.GetB()),
154 min_color(min(min(r, g), b)), max_color(max(max(r, g), b));
155 mid_t h(0); // 此处 h 的值每 0x6 对应一个圆周。
156 mid_t s(0);
157 decltype(hsl_t::l) l;
158
159 if(min_color == max_color)
160 l = decltype(hsl_t::l)(min_color) / 0x100;
161 else
162 {
163 const unsigned p(max_color + min_color);
164
165 l = decltype(hsl_t::l)(p) / 0x200;
166 /*
167 l = 0.2126 * r + 0.7152 * g + 0.0722 * b; // Rec. 601 luma;
168 l = 0.299 * r + 0.588 * g + 0.114 * b; // Rec. 709 luma;
169 */
170
171 // chroma * 256;
172 const mid_t q(max_color - min_color);
173
174 s = q / (p < 0x100 ? p : 0x200 - p);
175 if(r == max_color)
176 h = (g - b) / q;
177 else if(g == max_color)
178 h = (b - r) / q + 0x2;
179 else if(b == max_color)
180 h = (r - g) / q + 0x4;
181 if(h < 0)
182 h += 0x6;
183 }
184 return {h * 60, s, l};
185 }
186
187 Color
188 HSLToColor(hsl_t c)
189 {
190 if(c.s == 0)
191 return c.l > 255.F / 0x100 ? Color(0xFF, 0xFF, 0xFF)
192 : Color(c.l * 0x100, c.l * 0x100, c.l * 0x100);
193
194 typedef float mid_t; //中间类型。
195
196 mid_t t2((c.l < 0.5 ? c.l * (1 + c.s) : (c.l + c.s - c.l * c.s)) * 0x100),
197 t1((c.l * 0x200) - t2);
198 mid_t tmp[3] = {c.h + 120, c.h, c.h - 120}; \
199 // tmp 每个元素对应一个 RGB 分量，值 360 对应一个圆周。
200 float dc[3]; //对应 RGB 分量。
201
202 for(size_t i(0); i < 3; ++i)
203 {
204 if(tmp[i] < 0)
205 tmp[i] += 360;
206 else if(tmp[i] > 360)
207 tmp[i] -= 360;
208 if(tmp[i] < 60)
209 dc[i] = t1 + (t2 - t1) * tmp[i] / 60;
210 else if(tmp[i] < 180)
211 dc[i] = t2;
212 else if(tmp[i] < 240)
213 dc[i] = t1 + (t2 - t1) * (240 - tmp[i]) / 60;
214 else
215 dc[i] = t1;
216 if(dc[i] > 0xFF)
217 dc[i] = 0xFF;
218 }
219 return Color(dc[0], dc[1], dc[2]);
220 }
221
222 YSL_END_NAMESPACE(Drawing)
223
224 YSL_BEGIN_NAMESPACE(UI)
225
226 YSL_BEGIN_NAMESPACE(Styles)
227
228 Palette::Palette()
229 : colors{
230 {0, 0, 0}, // Null;
231 {10, 59, 118}, // Desktop;
232 {255, 255, 255}, // Window;
233 {240, 240, 240}, // Panel;
234 {237, 237, 237}, // Track;
235 {171, 171, 171}, // Workspace;
236 {160, 160, 160}, // Shadow;
237 {105, 105, 105}, // DockShadow;
238 {227, 227, 227}, // Light;
239 {100, 100, 100}, // Frame;
240 {51, 153, 255}, // Highlight;
241 {158, 62, 255}, // BorderFill;
242 ColorSpace::Aqua, // ActiveBorder;
243 {180, 180, 180}, // InactiveBorder;
244 {153, 180, 209}, // ActiveTitle;
245 {191, 205, 219}, // InactiveTitle;
246
247 {255, 255, 255}, // HighlightText;
248 {0, 0, 0}, // WindowText;
249 {0, 0, 0}, // PanelText;
250 {109, 109, 109}, // GrayText;
251 {0, 0, 0}, // TitleText;
252 {67, 78, 84}, // InactiveTitleText;
253 {0, 102, 204} // HotTracking;
254 }
255 {
256 // colors[ActiveBorder] = Color(180, 180, 180),
257 // colors[InactiveBorder] = Color(244, 247, 252);
258 //"GradientActiveTitle"="185 209 234"
259 //"GradientInactiveTitle"="215 228 242"
260 }
261
262 pair<Drawing::Color, Drawing::Color>
263 Palette::GetPair(Palette::ColorListType::size_type n1,
264 Palette::ColorListType::size_type n2) const
265 {
266 return make_pair(colors[n1], colors[n2]);
267 }
268
269 YSL_END_NAMESPACE(Styles)
270
271 YSL_END_NAMESPACE(UI)
272
273 YSL_END
274