Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: ui/views/color_chooser/color_chooser_view.cc

Issue 10442020: Initial implementation of ColorChooser for Aura. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/color_chooser/color_chooser_view.h ('k') | ui/views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/color_chooser/color_chooser_view.h"
6
7 #include "base/logging.h"
8 #include "base/string_number_conversions.h"
9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/base/keycodes/keyboard_codes.h"
13 #include "ui/views/color_chooser/color_chooser_listener.h"
14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/controls/textfield/textfield_controller.h"
16 #include "ui/views/events/event.h"
17 #include "ui/views/layout/box_layout.h"
18 #include "ui/views/widget/widget.h"
19
20 namespace {
21
22 const int kHueBarWidth = 20;
23 const int kSaturationValueSize = 200;
24 const int kMarginWidth = 5;
25 const int kSaturationValueIndicatorSize = 3;
26 const int kHueIndicatorSize = 3;
27
28 string16 GetColorText(SkColor color) {
29 return ASCIIToUTF16(base::StringPrintf("#%02x%02x%02x",
30 SkColorGetR(color),
31 SkColorGetG(color),
32 SkColorGetB(color)));
33 }
34
35 bool GetColorFromText(const string16& text, SkColor* result) {
36 if (text.size() != 6 && !(text.size() == 7 && text[0] == '#'))
37 return false;
38
39 std::string input = UTF16ToUTF8((text.size() == 6) ? text : text.substr(1));
40 std::vector<uint8> hex;
41 if (!base::HexStringToBytes(input, &hex))
42 return false;
43
44 *result = SkColorSetRGB(hex[0], hex[1], hex[2]);
45 return true;
46 }
47
48 } // namespace
49
50 namespace views {
51
52 // The class to choose the hue of the color. It draws a vertical bar and
53 // the indicator for the currently selected hue.
54 class ColorChooserView::HueView : public View {
55 public:
56 explicit HueView(ColorChooserView* chooser_view);
57
58 void OnHueChanged(SkScalar hue);
59
60 private:
61 // View overrides:
62 virtual gfx::Size GetPreferredSize() OVERRIDE;
63 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
64 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE;
65 virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE;
66
67 ColorChooserView* chooser_view_;
68 int level_;
69
70 DISALLOW_COPY_AND_ASSIGN(HueView);
71 };
72
73 ColorChooserView::HueView::HueView(ColorChooserView* chooser_view)
74 : chooser_view_(chooser_view),
75 level_(0) {
76 set_focusable(false);
77 }
78
79 void ColorChooserView::HueView::OnHueChanged(SkScalar hue) {
80 SkScalar height = SkIntToScalar(GetPreferredSize().height() - 1);
81 SkScalar hue_max = SkIntToScalar(360);
82 int level = SkScalarDiv(SkScalarMul(hue_max - hue, height), hue_max);
83 if (level_ != level) {
84 level_ = level;
85 SchedulePaint();
86 }
87 }
88
89 gfx::Size ColorChooserView::HueView::GetPreferredSize() {
90 // We put indicators on the both sides of the hue bar.
91 return gfx::Size(kHueBarWidth + kHueIndicatorSize * 2, kSaturationValueSize);
92 }
93
94 void ColorChooserView::HueView::OnPaint(gfx::Canvas* canvas) {
95 SkScalar hsv[3];
96 // In the hue bar, saturation and value for the color should be always 100%.
97 hsv[1] = SK_Scalar1;
98 hsv[2] = SK_Scalar1;
99
100 for (int y = 0; y < height(); ++y) {
101 hsv[0] = SkScalarDiv(SkScalarMul(SkIntToScalar(360),
102 SkIntToScalar(height() - 1 - y)),
103 SkIntToScalar(height() - 1));
104 canvas->DrawLine(gfx::Point(kHueIndicatorSize, y),
105 gfx::Point(width() - kHueIndicatorSize, y),
106 SkHSVToColor(hsv));
107 }
108
109 // Put the triangular indicators besides.
110 SkPath left_indicator_path;
111 SkPath right_indicator_path;
112 left_indicator_path.moveTo(
113 SK_ScalarHalf, SkIntToScalar(level_ - kHueIndicatorSize));
114 left_indicator_path.lineTo(
115 kHueIndicatorSize, SkIntToScalar(level_));
116 left_indicator_path.lineTo(
117 SK_ScalarHalf, SkIntToScalar(level_ + kHueIndicatorSize));
118 left_indicator_path.lineTo(
119 SK_ScalarHalf, SkIntToScalar(level_ - kHueIndicatorSize));
120 right_indicator_path.moveTo(
121 SkIntToScalar(width()) - SK_ScalarHalf,
122 SkIntToScalar(level_ - kHueIndicatorSize));
123 right_indicator_path.lineTo(
124 SkIntToScalar(width() - kHueIndicatorSize) - SK_ScalarHalf,
125 SkIntToScalar(level_));
126 right_indicator_path.lineTo(
127 SkIntToScalar(width()) - SK_ScalarHalf,
128 SkIntToScalar(level_ + kHueIndicatorSize));
129 right_indicator_path.lineTo(
130 SkIntToScalar(width()) - SK_ScalarHalf,
131 SkIntToScalar(level_ - kHueIndicatorSize));
132
133 SkPaint indicator_paint;
134 indicator_paint.setColor(SK_ColorBLACK);
135 indicator_paint.setStyle(SkPaint::kStroke_Style);
136 canvas->DrawPath(left_indicator_path, indicator_paint);
137 canvas->DrawPath(right_indicator_path, indicator_paint);
138 }
139
140 bool ColorChooserView::HueView::OnMousePressed(const MouseEvent& event) {
141 level_ = std::max(0, std::min(height() - 1, event.y()));
142 chooser_view_->OnHueChosen(SkScalarDiv(
143 SkScalarMul(SkIntToScalar(360), SkIntToScalar(height() - 1 - level_)),
144 SkIntToScalar(height() - 1)));
145 return true;
146 }
147
148 bool ColorChooserView::HueView::OnMouseDragged(const MouseEvent& event) {
149 return OnMousePressed(event);
150 }
151
152 // The class to choose the saturation and the value of the color. It draws
153 // a square area and the indicator for the currently selected saturation and
154 // value.
155 class ColorChooserView::SaturationValueView : public View {
156 public:
157 explicit SaturationValueView(ColorChooserView* chooser_view);
158
159 void OnHueChanged(SkScalar hue);
160 void OnSaturationValueChanged(SkScalar saturation, SkScalar value);
161
162 private:
163 // View overrides:
164 virtual gfx::Size GetPreferredSize() OVERRIDE;
165 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
166 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE;
167 virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE;
168
169 ColorChooserView* chooser_view_;
170 SkScalar hue_;
171 gfx::Point marker_position_;
172
173 DISALLOW_COPY_AND_ASSIGN(SaturationValueView);
174 };
175
176 ColorChooserView::SaturationValueView::SaturationValueView(
177 ColorChooserView* chooser_view)
178 : chooser_view_(chooser_view), hue_(0) {
179 set_focusable(false);
180 }
181
182 void ColorChooserView::SaturationValueView::OnHueChanged(SkScalar hue) {
183 if (hue_ != hue) {
184 hue_ = hue;
185 SchedulePaint();
186 }
187 }
188
189 void ColorChooserView::SaturationValueView::OnSaturationValueChanged(
190 SkScalar saturation,
191 SkScalar value) {
192 SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1);
193 int x = SkScalarFloorToInt(SkScalarMul(saturation, scalar_size));
194 int y = SkScalarFloorToInt(SkScalarMul(SK_Scalar1 - value, scalar_size));
195 if (gfx::Point(x, y) == marker_position_)
196 return;
197
198 marker_position_.set_x(x);
199 marker_position_.set_y(y);
200 SchedulePaint();
201 chooser_view_->OnSaturationValueChosen(saturation, value);
202 }
203
204 gfx::Size ColorChooserView::SaturationValueView::GetPreferredSize() {
205 return gfx::Size(kSaturationValueSize, kSaturationValueSize);
206 }
207
208 void ColorChooserView::SaturationValueView::OnPaint(gfx::Canvas* canvas) {
209 SkScalar hsv[3];
210 hsv[0] = hue_;
211 SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1);
212 for (int x = 0; x < width(); ++x) {
213 hsv[1] = SkScalarDiv(SkIntToScalar(x), scalar_size);
214 for (int y = 0; y < height(); ++y) {
215 hsv[2] = SK_Scalar1 - SkScalarDiv(SkIntToScalar(y), scalar_size);
216 SkPaint paint;
217 paint.setColor(SkHSVToColor(255, hsv));
218 canvas->DrawPoint(gfx::Point(x, y), paint);
219 }
220 }
221
222 // The background is very dark at the bottom of the view. Use a white
223 // marker in that case.
224 SkColor indicator_color =
225 (marker_position_.y() > width() * 3 / 4) ? SK_ColorWHITE : SK_ColorBLACK;
226 // Draw a crosshair indicator but do not draw its center to see the selected
227 // saturation/value. Note that the DrawLine() doesn't draw the right-bottom
228 // pixel.
229 canvas->DrawLine(
230 gfx::Point(marker_position_.x(),
231 marker_position_.y() - kSaturationValueIndicatorSize),
232 gfx::Point(marker_position_.x(),
233 marker_position_.y()),
234 indicator_color);
235 canvas->DrawLine(
236 gfx::Point(marker_position_.x(),
237 marker_position_.y() + kSaturationValueIndicatorSize + 1),
238 gfx::Point(marker_position_.x(),
239 marker_position_.y() + 1),
240 indicator_color);
241 canvas->DrawLine(
242 gfx::Point(marker_position_.x() - kSaturationValueIndicatorSize,
243 marker_position_.y()),
244 gfx::Point(marker_position_.x(),
245 marker_position_.y()),
246 indicator_color);
247 canvas->DrawLine(
248 gfx::Point(marker_position_.x() + kSaturationValueIndicatorSize + 1,
249 marker_position_.y()),
250 gfx::Point(marker_position_.x() + 1,
251 marker_position_.y()),
252 indicator_color);
253 }
254
255 bool ColorChooserView::SaturationValueView::OnMousePressed(
256 const MouseEvent& event) {
257 SkScalar scalar_size = SkIntToScalar(kSaturationValueSize);
258 SkScalar saturation = SkScalarDiv(SkIntToScalar(event.x()), scalar_size);
259 SkScalar value = SK_Scalar1 - SkScalarDiv(
260 SkIntToScalar(event.y()), scalar_size);
261 saturation = SkScalarPin(saturation, 0, SK_Scalar1);
262 value = SkScalarPin(value, 0, SK_Scalar1);
263 OnSaturationValueChanged(saturation, value);
264 return true;
265 }
266
267 bool ColorChooserView::SaturationValueView::OnMouseDragged(
268 const MouseEvent& event) {
269 return OnMousePressed(event);
270 }
271
272
273 ColorChooserView::ColorChooserView(ColorChooserListener* listener,
274 SkColor initial_color)
275 : listener_(listener) {
276 DCHECK(listener_);
277
278 set_focusable(false);
279 set_background(Background::CreateSolidBackground(SK_ColorLTGRAY));
280 SetLayoutManager(new BoxLayout(BoxLayout::kVertical, kMarginWidth,
281 kMarginWidth, kMarginWidth));
282
283 View* container = new View();
284 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0,
285 kMarginWidth));
286 saturation_value_ = new SaturationValueView(this);
287 container->AddChildView(saturation_value_);
288 hue_ = new HueView(this);
289 container->AddChildView(hue_);
290 AddChildView(container);
291
292 textfield_ = new Textfield();
293 textfield_->SetController(this);
294 AddChildView(textfield_);
295
296 OnColorChanged(initial_color);
297 }
298
299 ColorChooserView::~ColorChooserView() {
300 }
301
302 void ColorChooserView::OnColorChanged(SkColor color) {
303 SkColorToHSV(color, hsv_);
304 hue_->OnHueChanged(hsv_[0]);
305 saturation_value_->OnHueChanged(hsv_[0]);
306 saturation_value_->OnSaturationValueChanged(hsv_[1], hsv_[2]);
307 textfield_->SetText(GetColorText(color));
308 }
309
310 void ColorChooserView::OnHueChosen(SkScalar hue) {
311 hsv_[0] = hue;
312 SkColor color = SkHSVToColor(255, hsv_);
313 listener_->OnColorChosen(color);
314 saturation_value_->OnHueChanged(hue);
315 textfield_->SetText(GetColorText(color));
316 }
317
318 void ColorChooserView::OnSaturationValueChosen(SkScalar saturation,
319 SkScalar value) {
320 hsv_[1] = saturation;
321 hsv_[2] = value;
322 SkColor color = SkHSVToColor(255, hsv_);
323 listener_->OnColorChosen(color);
324 textfield_->SetText(GetColorText(color));
325 }
326
327 View* ColorChooserView::GetInitiallyFocusedView() {
328 return textfield_;
329 }
330
331 ui::ModalType ColorChooserView::GetModalType() const {
332 return ui::MODAL_TYPE_WINDOW;
333 }
334
335 void ColorChooserView::WindowClosing() {
336 if (listener_)
337 listener_->OnColorChooserDialogClosed();
338 }
339
340 View* ColorChooserView::GetContentsView() {
341 return this;
342 }
343
344 void ColorChooserView::ContentsChanged(Textfield* sender,
345 const string16& new_contents) {
346 SkColor color = SK_ColorBLACK;
347 if (GetColorFromText(new_contents, &color)) {
348 SkColorToHSV(color, hsv_);
349 listener_->OnColorChosen(color);
350 hue_->OnHueChanged(hsv_[0]);
351 saturation_value_->OnHueChanged(hsv_[0]);
352 saturation_value_->OnSaturationValueChanged(hsv_[1], hsv_[2]);
353 }
354 }
355
356 bool ColorChooserView::HandleKeyEvent(Textfield* sender,
357 const KeyEvent& key_event) {
358 if (key_event.key_code() != ui::VKEY_RETURN &&
359 key_event.key_code() != ui::VKEY_ESCAPE)
360 return false;
361
362 GetWidget()->Close();
363 return true;
364 }
365
366 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/color_chooser/color_chooser_view.h ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698