forked from HaikuArchives/ArtPaint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorSelectorTool.cpp
More file actions
500 lines (394 loc) · 12.9 KB
/
Copy pathColorSelectorTool.cpp
File metadata and controls
500 lines (394 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright 2003, Heikki Suhonen
* Copyright 2009, Karsten Heimrich
* Distributed under the terms of the MIT License.
*
* Authors:
* Heikki Suhonen <heikki.suhonen@gmail.com>
* Karsten Heimrich <host.haiku@gmx.de>
* Dale Cieslak <dcieslak@yahoo.com>
*
*/
#include "ColorSelectorTool.h"
#include "BitmapDrawer.h"
#include "BitmapUtilities.h"
#include "ColorPalette.h"
#include "Cursors.h"
#include "Image.h"
#include "ImageView.h"
#include "MessageConstants.h"
#include "NumberSliderControl.h"
#include "PaintApplication.h"
#include "SettingsServer.h"
#include "StatusView.h"
#include "UtilityClasses.h"
#include <Bitmap.h>
#include <Catalog.h>
#include <GridLayout.h>
#include <GroupLayoutBuilder.h>
#include <InterfaceDefs.h>
#include <RadioButton.h>
#include <Screen.h>
#include <StringView.h>
#include <Window.h>
#include <stdio.h>
#include <stdlib.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Tools"
using ArtPaint::Interface::NumberSliderControl;
// #pragma mark -- ColorSelectorView
class ColorSelectorView : public BView
{
public:
ColorSelectorView(BRect frame);
virtual void Draw(BRect updateRect);
void ChangeValue(uint32);
private:
uint32 selected_color;
BStringView* red_view;
BStringView* green_view;
BStringView* blue_view;
BStringView* alpha_view;
};
ColorSelectorView::ColorSelectorView(BRect frame)
:
BView(frame, "color selector-view", B_FOLLOW_NONE, B_WILL_DRAW)
{
BString color_str;
float width = 0;
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
color_str.SetToFormat("%s:", B_TRANSLATE("Red"));
BStringView* label_view = new BStringView(BRect(2, 2, 2, 2), "label view", color_str);
font_height fHeight;
label_view->GetFontHeight(&fHeight);
color_str.SetToFormat(" %s:", B_TRANSLATE("Red"));
width = label_view->StringWidth(color_str);
color_str.SetToFormat(" %s:", B_TRANSLATE("Green"));
width = max_c(width, label_view->StringWidth(color_str));
color_str.SetToFormat(" %s:", B_TRANSLATE("Blue"));
width = max_c(width, label_view->StringWidth(color_str));
color_str.SetToFormat(" %s:", B_TRANSLATE("Alpha"));
width = max_c(width, label_view->StringWidth(color_str));
label_view->ResizeTo(width, fHeight.ascent + fHeight.descent);
label_view->SetAlignment(B_ALIGN_RIGHT);
AddChild(label_view);
// The red color-view
BRect label_frame = label_view->Frame();
label_frame.OffsetBy(label_frame.Width(), 0);
red_view = new BStringView(label_frame, "red view", "");
AddChild(red_view);
color_str.SetToFormat("%s:", B_TRANSLATE("Green"));
label_view
= new BStringView(BRect(2, label_view->Frame().bottom, 2, label_view->Frame().bottom),
"label view", color_str);
label_view->ResizeTo(width, fHeight.ascent + fHeight.descent);
label_view->SetAlignment(B_ALIGN_RIGHT);
AddChild(label_view);
label_frame = label_view->Frame();
label_frame.OffsetBy(label_frame.Width(), 0);
green_view = new BStringView(label_frame, "green view", "");
AddChild(green_view);
color_str.SetToFormat("%s:", B_TRANSLATE("Blue"));
label_view
= new BStringView(BRect(2, label_view->Frame().bottom, 2, label_view->Frame().bottom),
"label view", color_str);
label_view->ResizeTo(width, fHeight.ascent + fHeight.descent);
label_view->SetAlignment(B_ALIGN_RIGHT);
AddChild(label_view);
label_frame = label_view->Frame();
label_frame.OffsetBy(label_frame.Width(), 0);
blue_view = new BStringView(label_frame, "blue view", "");
AddChild(blue_view);
color_str.SetToFormat(" %s:", B_TRANSLATE("Alpha"));
label_view
= new BStringView(BRect(2, label_view->Frame().bottom, 2, label_view->Frame().bottom),
"label view", color_str);
label_view->ResizeTo(width, fHeight.ascent + fHeight.descent);
label_view->SetAlignment(B_ALIGN_RIGHT);
AddChild(label_view);
label_frame = label_view->Frame();
label_frame.OffsetBy(label_frame.Width(), 0);
alpha_view = new BStringView(label_frame, "alpha view", "");
AddChild(alpha_view);
ResizeTo(alpha_view->Frame().right + 40, alpha_view->Frame().bottom + 2);
}
void
ColorSelectorView::Draw(BRect area)
{
BView::Draw(area);
BRect color_rect = BRect(red_view->Frame().right + 2, red_view->Frame().top + 2,
Bounds().right - 2, alpha_view->Frame().bottom - 2);
SetHighColor(120, 120, 0, 255);
StrokeRect(color_rect, B_MIXED_COLORS);
color_rect.InsetBy(1, 1);
uint32 color1, color2;
rgb_color rgb1, rgb2;
rgb1.red = rgb1.green = rgb1.blue = 0xBB;
rgb2.red = rgb2.green = rgb2.blue = 0x99;
rgb1.alpha = rgb2.alpha = 0xFF;
color1 = RGBColorToBGRA(rgb1);
color2 = RGBColorToBGRA(rgb2);
if (SettingsServer* server = SettingsServer::Instance()) {
BMessage settings;
server->GetApplicationSettings(&settings);
color1 = settings.GetUInt32(skBgColor1, color1);
color2 = settings.GetUInt32(skBgColor2, color2);
}
BBitmap colorBitmap(BRect(0.0, 0.0, 15.0, 15.0), B_RGBA32);
BitmapUtilities::CheckerBitmap(&colorBitmap, color1, color2, 8);
BBitmap tmp(colorBitmap);
BitmapUtilities::ClearBitmap(&tmp, selected_color);
BitmapUtilities::CompositeBitmapOnSource(&colorBitmap, &colorBitmap, &tmp, tmp.Bounds());
DrawTiledBitmap(&colorBitmap, color_rect, color_rect.LeftTop());
}
void
ColorSelectorView::ChangeValue(uint32 new_color)
{
union {
uint8 bytes[4];
uint32 word;
} color;
color.word = new_color;
selected_color = new_color;
Draw(Bounds());
char a_string[20];
sprintf(a_string, "%d", color.bytes[2]);
red_view->SetText(a_string);
sprintf(a_string, "%d", color.bytes[1]);
green_view->SetText(a_string);
sprintf(a_string, "%d", color.bytes[0]);
blue_view->SetText(a_string);
sprintf(a_string, "%d", color.bytes[3]);
alpha_view->SetText(a_string);
}
// #pragma mark -- ColorSelectorWindow
class ColorSelectorWindow : public BWindow
{
public:
ColorSelectorWindow(BPoint cursorLocation);
void ChangeValue(uint32 color);
void Move(BPoint cursorLocation);
private:
BRect screen_bounds;
ColorSelectorView* cs_view;
};
ColorSelectorWindow::ColorSelectorWindow(BPoint cursor_location)
:
BWindow(BRect(0, 0, 0, 0), B_TRANSLATE("Color picker window"), B_BORDERED_WINDOW, 0)
{
screen_bounds = BScreen().Frame();
cs_view = new ColorSelectorView(Bounds());
AddChild(cs_view);
// Here we must resize the window and move it to the correct position.
ResizeTo(cs_view->Frame().Width(), cs_view->Frame().Height());
Move(cursor_location);
Show();
}
void
ColorSelectorWindow::ChangeValue(uint32 color)
{
cs_view->ChangeValue(color);
}
void
ColorSelectorWindow::Move(BPoint cursor_location)
{
// This function moves the window so that it is near the cursor, but does not
// interfere with it and does not go over screen borders. The cursor location
// is expressed in screen-coordinates.
// See if there is enough space on the same side of the cursor than we are now.
float width = Bounds().Width();
float height = Bounds().Height();
float left, top;
if (cursor_location.x > width + 20)
left = cursor_location.x - width - 20;
else
left = cursor_location.x + 20;
if (cursor_location.y > height + 20)
top = cursor_location.y - height - 20;
else
top = cursor_location.y + 20;
MoveTo(BPoint(left, top));
}
// #pragma mark -- ColorSelectorTool
ColorSelectorTool::ColorSelectorTool()
:
DrawingTool(B_TRANSLATE("Color picker tool"), "c", COLOR_SELECTOR_TOOL)
{
fOptions = SIZE_OPTION | MODE_OPTION;
fOptionsCount = 2;
SetOption(SIZE_OPTION, 1);
SetOption(MODE_OPTION, 0);
fToolSettings.size = 1;
fToolSettings.mode = 0;
}
ToolScript*
ColorSelectorTool::UseTool(ImageView* view, uint32 buttons, BPoint point, BPoint view_point)
{
BWindow* window = view->Window();
BBitmap* bitmap;
if (window != NULL) {
if (fToolSettings.mode == 0)
bitmap = view->ReturnImage()->ReturnActiveBitmap();
else
bitmap = view->ReturnImage()->ReturnRenderedImage();
BitmapDrawer* drawer = new BitmapDrawer(bitmap);
BPoint original_point, original_view_point, prev_view_point;
ColorSelectorWindow* cs_window = NULL;
original_point = point;
prev_view_point = original_view_point = view_point;
uint32 old_color, color;
color = drawer->GetPixel(point);
old_color = color - 1;
bool select_foreground = (buttons & B_PRIMARY_MOUSE_BUTTON) != 0x00;
int32 half_size = fToolSettings.size / 2.;
BRect rc
= BRect(point.x, point.y, point.x + fToolSettings.size, point.y + fToolSettings.size);
rc.OffsetBySelf(-half_size, -half_size);
float scale = view->getMagScale();
BRect view_rc = view->convertBitmapRectToView(rc);
view_rc.right -= scale;
view_rc.bottom -= scale;
BRect old_rc = view_rc;
BRect bounds = bitmap->Bounds();
rc = rc & bounds;
while (buttons) {
rc = BRect(
point.x, point.y, point.x + fToolSettings.size, point.y + fToolSettings.size);
rc.OffsetBySelf(-half_size, -half_size);
rc = rc & bounds;
view_rc = view->convertBitmapRectToView(rc);
scale = view->getMagScale();
view_rc.right -= scale;
view_rc.bottom -= scale;
if (view->LockLooper() == true) {
if (old_rc != view_rc) {
old_rc.InsetBySelf(-2, -2);
view->Draw(old_rc);
view->StrokeRect(view_rc, B_SOLID_HIGH);
view->StrokeRect(view_rc.InsetByCopy(-1, -1), B_SOLID_LOW);
old_rc = view_rc;
}
view->UnlockLooper();
}
int32 x_dist, y_sqr;
int32 width = rc.IntegerWidth();
int32 height = rc.IntegerHeight();
float red = 0;
float green = 0;
float blue = 0;
float alpha = 0;
int32 number_of_pixels = 0;
for (int32 y = 0; y < height; y++) {
int32 real_y = (int32)(rc.top + y);
int32 real_x;
for (int32 x = 0; x < width; x++) {
real_x = (int32)(rc.left + x);
uint32 tmp_color = drawer->GetPixel(real_x, real_y);
red += (tmp_color >> 8) & 0xFF;
green += (tmp_color >> 16) & 0xFF;
blue += (tmp_color >> 24) & 0xFF;
alpha += (tmp_color) &0xFF;
number_of_pixels++;
}
}
if (number_of_pixels > 0) {
red /= number_of_pixels;
green /= number_of_pixels;
blue /= number_of_pixels;
alpha /= number_of_pixels;
color = (((uint32)blue) << 24) | (((uint32)green) << 16) | (((uint32)red) << 8)
| ((uint32)alpha);
}
window->Lock();
view->getCoords(&point, &buttons, &view_point);
// If we have not yet opened the cs window and the user moves the mouse,
// we open the window
if ((cs_window == NULL)
&& ((fabs(point.x - original_point.x) > 4)
|| (fabs(point.y - original_point.y) > 4))) {
cs_window = new ColorSelectorWindow(view->ConvertToScreen(view_point));
}
// If we have opened the cs_window, we can operate on it.
if (cs_window != NULL) {
cs_window->Lock();
if (color != old_color) {
cs_window->ChangeValue(color);
old_color = color;
rgb_color c = BGRAColorToRGB(color);
}
if (cs_window->Frame().Contains(view->ConvertToScreen(view_point)))
cs_window->Move(view->ConvertToScreen(view_point));
cs_window->Unlock();
}
window->Unlock();
snooze(20 * 1000);
}
if (view->LockLooper() == true) {
view->Draw(view_rc.InsetByCopy(-1, -1));
view->UnlockLooper();
}
// Close the color picker window
if (cs_window != NULL) {
cs_window->Lock();
cs_window->Quit();
window->Lock();
window->Activate(true);
window->Unlock();
}
rgb_color new_color = BGRAColorToRGB(color);
((PaintApplication*)be_app)->SetColor(new_color, select_foreground);
ColorPaletteWindow::ChangePaletteColor(new_color);
// Inform all the selected color views about change in colors.
SelectedColorsView::SendMessageToAll(HS_COLOR_CHANGED);
delete drawer;
}
return NULL;
}
BView*
ColorSelectorTool::ConfigView()
{
return new ColorSelectorToolConfigView(this);
}
const void*
ColorSelectorTool::ToolCursor() const
{
return HS_COLOR_SELECTOR_CURSOR;
}
const char*
ColorSelectorTool::HelpString(bool isInUse) const
{
return (isInUse ? B_TRANSLATE("Picking a color.") : B_TRANSLATE("Color picker tool"));
}
// #pragma mark -- ColorSelectorToolConfigView
ColorSelectorToolConfigView::ColorSelectorToolConfigView(DrawingTool* tool)
:
DrawingToolConfigView(tool)
{
if (BLayout* layout = GetLayout()) {
BMessage* message = new BMessage(OPTION_CHANGED);
message->AddInt32("option", SIZE_OPTION);
message->AddInt32("value", tool->GetCurrentValue(SIZE_OPTION));
fSizeSlider = new NumberSliderControl(B_TRANSLATE("Size:"), "1", message, 1, 10, false);
BMessage* all_layers_message = new BMessage(OPTION_CHANGED);
all_layers_message->AddInt32("option", MODE_OPTION);
all_layers_message->AddInt32("value", tool->GetCurrentValue(MODE_OPTION));
fAllLayersCheckbox = new BCheckBox(B_TRANSLATE("Sample all layers"), all_layers_message);
BGridLayout* sizeLayout = LayoutSliderGrid(fSizeSlider);
layout->AddView(BGroupLayoutBuilder(B_VERTICAL, kWidgetSpacing)
.Add(sizeLayout)
.Add(fAllLayersCheckbox)
.TopView()
);
}
if (tool->GetCurrentValue(MODE_OPTION) == 1)
fAllLayersCheckbox->SetValue(B_CONTROL_ON);
else
fAllLayersCheckbox->SetValue(B_CONTROL_OFF);
}
void
ColorSelectorToolConfigView::AttachedToWindow()
{
DrawingToolConfigView::AttachedToWindow();
fSizeSlider->SetTarget(this);
fAllLayersCheckbox->SetTarget(this);
}