~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to src/dialog_colorpicker.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2005, Niels Martin Hansen
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are met:
 
6
//
 
7
//   * Redistributions of source code must retain the above copyright notice,
 
8
//     this list of conditions and the following disclaimer.
 
9
//   * Redistributions in binary form must reproduce the above copyright notice,
 
10
//     this list of conditions and the following disclaimer in the documentation
 
11
//     and/or other materials provided with the distribution.
 
12
//   * Neither the name of the Aegisub Group nor the names of its contributors
 
13
//     may be used to endorse or promote products derived from this software
 
14
//     without specific prior written permission.
 
15
//
 
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
// POSSIBILITY OF SUCH DAMAGE.
 
27
//
 
28
// -----------------------------------------------------------------------------
 
29
//
 
30
// AEGISUB
 
31
//
 
32
// Website: http://aegisub.cellosoft.com
 
33
// Contact: mailto:zeratul@cellosoft.com
 
34
//
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <wx/image.h>
 
39
#include <wx/statbox.h>
 
40
#include <wx/stattext.h>
 
41
#include <wx/sizer.h>
 
42
#include <wx/gbsizer.h>
 
43
#include <wx/event.h>
 
44
#include <wx/tokenzr.h>
 
45
#include <wx/dcclient.h>
 
46
#include <wx/dcmemory.h>
 
47
#include <wx/dcscreen.h>
 
48
#include <wx/settings.h>
 
49
#include <wx/clipbrd.h>
 
50
#include "dialog_colorpicker.h"
 
51
#include "colorspace.h"
 
52
#include "ass_style.h"
 
53
#include "options.h"
 
54
#include "help_button.h"
 
55
#include "utils.h"
 
56
 
 
57
#include <stdio.h>
 
58
 
 
59
 
 
60
#ifdef WIN32
 
61
#define STATIC_BORDER_FLAG wxSTATIC_BORDER
 
62
#else
 
63
#define STATIC_BORDER_FLAG wxSIMPLE_BORDER
 
64
#endif
 
65
 
 
66
 
 
67
static const int spectrum_horz_vert_arrow_size = 4;
 
68
 
 
69
ColorPickerSpectrum::ColorPickerSpectrum(wxWindow *parent, wxWindowID id, wxBitmap *_background, int xx, int yy, PickerDirection _direction, wxSize _size)
 
70
: wxControl(parent, id, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE), x(xx), y(yy), background(_background), direction(_direction)
 
71
{
 
72
        _size.x += 2;
 
73
        _size.y += 2;
 
74
 
 
75
        if (direction == Vert) _size.x += spectrum_horz_vert_arrow_size + 1;
 
76
        if (direction == Horz) _size.y += spectrum_horz_vert_arrow_size + 1;
 
77
 
 
78
        SetClientSize(_size);
 
79
        SetMinSize(GetSize());
 
80
}
 
81
 
 
82
void ColorPickerSpectrum::GetXY(int &xx, int &yy)
 
83
{
 
84
        xx = x;
 
85
        yy = y;
 
86
}
 
87
 
 
88
void ColorPickerSpectrum::SetXY(int xx, int yy)
 
89
{
 
90
        x = xx;
 
91
        y = yy;
 
92
        Refresh(true);
 
93
}
 
94
 
 
95
void ColorPickerSpectrum::SetBackground(wxBitmap *new_background)
 
96
{
 
97
        if (background == new_background) return;
 
98
        background = new_background;
 
99
        Refresh(true);
 
100
}
 
101
 
 
102
BEGIN_EVENT_TABLE(ColorPickerSpectrum, wxControl)
 
103
        EVT_PAINT(ColorPickerSpectrum::OnPaint)
 
104
        EVT_MOUSE_EVENTS(ColorPickerSpectrum::OnMouse)
 
105
END_EVENT_TABLE()
 
106
 
 
107
DEFINE_EVENT_TYPE(wxSPECTRUM_CHANGE)
 
108
 
 
109
void ColorPickerSpectrum::OnPaint(wxPaintEvent &evt)
 
110
{
 
111
        if (!background) return;
 
112
 
 
113
        wxPaintDC dc(this);
 
114
 
 
115
        wxMemoryDC memdc;
 
116
        memdc.SelectObject(*background);
 
117
        dc.Blit(1, 1, background->GetWidth(), background->GetHeight(), &memdc, 0, 0);
 
118
 
 
119
        wxPen invpen(*wxWHITE, 3);
 
120
        invpen.SetCap(wxCAP_BUTT);
 
121
        wxPen blkpen(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT), 1);
 
122
        blkpen.SetCap(wxCAP_BUTT);
 
123
 
 
124
        wxPoint arrow[3];
 
125
 
 
126
        dc.SetLogicalFunction(wxXOR);
 
127
        dc.SetPen(invpen);
 
128
        switch (direction) {
 
129
                case HorzVert:
 
130
                        // Make a little cross
 
131
                        dc.DrawLine(x-4, y+1, x+7, y+1);
 
132
                        dc.DrawLine(x+1, y-4, x+1, y+7);
 
133
                        break;
 
134
                case Horz:
 
135
                        // Make a vertical line stretching all the way across
 
136
                        dc.DrawLine(x+1, 1, x+1, background->GetHeight()+1);
 
137
                        // Points for arrow
 
138
                        arrow[0] = wxPoint(x+1, background->GetHeight()+2);
 
139
                        arrow[1] = wxPoint(x+1-spectrum_horz_vert_arrow_size, background->GetHeight()+2+spectrum_horz_vert_arrow_size);
 
140
                        arrow[2] = wxPoint(x+1+spectrum_horz_vert_arrow_size, background->GetHeight()+2+spectrum_horz_vert_arrow_size);
 
141
                        break;
 
142
                case Vert:
 
143
                        // Make a horizontal line stretching all the way across
 
144
                        dc.DrawLine(1, y+1, background->GetWidth()+1, y+1);
 
145
                        // Points for arrow
 
146
                        arrow[0] = wxPoint(background->GetWidth()+2, y+1);
 
147
                        arrow[1] = wxPoint(background->GetWidth()+2+spectrum_horz_vert_arrow_size, y+1-spectrum_horz_vert_arrow_size);
 
148
                        arrow[2] = wxPoint(background->GetWidth()+2+spectrum_horz_vert_arrow_size, y+1+spectrum_horz_vert_arrow_size);
 
149
                        break;
 
150
        }
 
151
 
 
152
        if (direction == Horz || direction == Vert) {
 
153
                // Arrow pointing at current point
 
154
                dc.SetLogicalFunction(wxCOPY);
 
155
                dc.SetPen(*wxTRANSPARENT_PEN);
 
156
                dc.SetBrush(*wxBLACK_BRUSH);
 
157
                dc.DrawPolygon(3, arrow);
 
158
        }
 
159
 
 
160
        // Border around the spectrum
 
161
        dc.SetLogicalFunction(wxCOPY);
 
162
        dc.SetPen(blkpen);
 
163
        dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
164
        dc.DrawRectangle(0, 0, background->GetWidth()+2, background->GetHeight()+2);
 
165
}
 
166
 
 
167
void ColorPickerSpectrum::OnMouse(wxMouseEvent &evt)
 
168
{
 
169
        evt.Skip();
 
170
 
 
171
        if (!evt.IsButton() && !evt.Dragging()) {
 
172
                return;
 
173
        }
 
174
 
 
175
        int newx = evt.GetX();
 
176
        if (newx < 0) newx = 0;
 
177
        if (newx >= GetClientSize().x) newx = GetClientSize().x-1;
 
178
        int newy = evt.GetY();
 
179
        if (newy < 0) newy = 0;
 
180
        if (newy >= GetClientSize().y) newy = GetClientSize().y-1;
 
181
 
 
182
        if (evt.LeftDown()) {
 
183
                CaptureMouse();
 
184
                SetCursor(wxCursor(wxCURSOR_BLANK));
 
185
        } else if (evt.LeftUp() && HasCapture()) {
 
186
                ReleaseMouse();
 
187
                SetCursor(wxNullCursor);
 
188
        }
 
189
 
 
190
        if (evt.LeftDown() || (HasCapture() && evt.LeftIsDown())) {
 
191
                x = newx;
 
192
                y = newy;
 
193
                Refresh(false);
 
194
                wxCommandEvent evt2(wxSPECTRUM_CHANGE, GetId());
 
195
                AddPendingEvent(evt2);
 
196
        }
 
197
}
 
198
 
 
199
 
 
200
 
 
201
ColorPickerRecent::ColorPickerRecent(wxWindow *parent, wxWindowID id, int _cols, int _rows, int _cellsize)
 
202
: wxControl(parent, id, wxDefaultPosition, wxDefaultSize, STATIC_BORDER_FLAG)
 
203
, rows(_rows)
 
204
, cols(_cols)
 
205
, cellsize(_cellsize)
 
206
, internal_control_offset(0,0)
 
207
, background_valid(false)
 
208
, background()
 
209
{
 
210
        LoadFromString(wxEmptyString);
 
211
        SetClientSize(cols*cellsize, rows*cellsize);
 
212
        SetMinSize(GetSize());
 
213
        SetMaxSize(GetSize());
 
214
        SetCursor(*wxCROSS_CURSOR);
 
215
}
 
216
 
 
217
void ColorPickerRecent::LoadFromString(const wxString &recent_string)
 
218
{
 
219
        colors.clear();
 
220
        wxStringTokenizer toker(recent_string, _T(" "), false);
 
221
        while (toker.HasMoreTokens()) {
 
222
                AssColor color;
 
223
                color.Parse(toker.NextToken());
 
224
                color.a = 0; // opaque
 
225
                colors.push_back(color.GetWXColor());
 
226
        }
 
227
        while ((int)colors.size() < rows*cols) {
 
228
                colors.push_back(*wxBLACK);
 
229
        }
 
230
        
 
231
        background_valid = false;
 
232
}
 
233
 
 
234
wxString ColorPickerRecent::StoreToString()
 
235
{
 
236
        wxString res;
 
237
        for (int i = 0; i < rows*cols; i++) {
 
238
                AssColor color(colors[i]);
 
239
                res << color.GetASSFormatted(false, false, false) << _T(" ");
 
240
        }
 
241
        res.Trim(true);
 
242
        return res;
 
243
}
 
244
 
 
245
void ColorPickerRecent::AddColor(wxColour color)
 
246
{
 
247
        for (std::vector<wxColor>::iterator i = colors.begin(); i != colors.end(); ++i) {
 
248
                if (color == *i) {
 
249
                        colors.erase(i);
 
250
                        break;
 
251
                }
 
252
        }
 
253
        
 
254
        colors.insert(colors.begin(), color);
 
255
        
 
256
        background_valid = false;
 
257
        
 
258
        Refresh(false);
 
259
}
 
260
 
 
261
BEGIN_EVENT_TABLE(ColorPickerRecent, wxControl)
 
262
        EVT_PAINT(ColorPickerRecent::OnPaint)
 
263
        EVT_LEFT_DOWN(ColorPickerRecent::OnClick)
 
264
        EVT_SIZE(ColorPickerRecent::OnSize)
 
265
END_EVENT_TABLE()
 
266
 
 
267
DEFINE_EVENT_TYPE(wxRECENT_SELECT)
 
268
 
 
269
void ColorPickerRecent::OnClick(wxMouseEvent &evt)
 
270
{
 
271
        int cx, cy, i;
 
272
        wxSize cs = GetClientSize();
 
273
        cx = (evt.GetX() - internal_control_offset.x) * cols / cs.x;
 
274
        cy = (evt.GetY() - internal_control_offset.y) * rows / cs.y;
 
275
        if (cx < 0 || cx > cols || cy < 0 || cy > rows) return;
 
276
        i = cols*cy + cx;
 
277
        if (i >= 0 && i < (int)colors.size()) {
 
278
                AssColor color(colors[i]);
 
279
                wxCommandEvent evt(wxRECENT_SELECT, GetId());
 
280
                evt.SetString(color.GetASSFormatted(false, false, false));
 
281
                AddPendingEvent(evt);
 
282
        }
 
283
}
 
284
 
 
285
void ColorPickerRecent::OnPaint(wxPaintEvent &evt)
 
286
{
 
287
        wxPaintDC pdc(this);
 
288
        PrepareDC(pdc);
 
289
 
 
290
        if (!background_valid) {
 
291
                wxSize sz = pdc.GetSize();
 
292
        
 
293
                background = wxBitmap(sz.x, sz.y);
 
294
                wxMemoryDC dc(background);
 
295
                
 
296
                int i = 0;
 
297
                dc.SetPen(*wxTRANSPARENT_PEN);
 
298
        
 
299
                for (int cy = 0; cy < rows; cy++) {
 
300
                        for (int cx = 0; cx < cols; cx++) {
 
301
                                int x, y;
 
302
                                x = cx * cellsize + internal_control_offset.x;
 
303
                                y = cy * cellsize + internal_control_offset.y;
 
304
        
 
305
                                dc.SetBrush(wxBrush(colors[i]));
 
306
                                dc.DrawRectangle(x, y, x+cellsize, y+cellsize);
 
307
        
 
308
                                i++;
 
309
                        }
 
310
                }
 
311
                
 
312
                background_valid = true;
 
313
        }
 
314
        
 
315
        pdc.DrawBitmap(background, 0, 0, false);
 
316
}
 
317
 
 
318
void ColorPickerRecent::OnSize(wxSizeEvent &evt)
 
319
{
 
320
        wxSize size = GetClientSize();
 
321
        background_valid = false;
 
322
        //internal_control_offset.x = (size.GetWidth() - cellsize * cols) / 2;
 
323
        //internal_control_offset.y = (size.GetHeight() - cellsize * rows) / 2;
 
324
        Refresh();
 
325
}
 
326
 
 
327
 
 
328
 
 
329
ColorPickerScreenDropper::ColorPickerScreenDropper(wxWindow *parent, wxWindowID id, int _resx, int _resy, int _magnification, bool _integrated_dropper)
 
330
: wxControl(parent, id, wxDefaultPosition, wxDefaultSize, STATIC_BORDER_FLAG), resx(_resx), resy(_resy), magnification(_magnification), integrated_dropper(_integrated_dropper)
 
331
{
 
332
        SetClientSize(resx*magnification, resy*magnification);
 
333
        SetMinSize(GetSize());
 
334
        SetMaxSize(GetSize());
 
335
        SetCursor(*wxCROSS_CURSOR);
 
336
 
 
337
        capture = wxBitmap(resx, resy);
 
338
        wxMemoryDC capdc;
 
339
        capdc.SelectObject(capture);
 
340
        capdc.SetPen(*wxTRANSPARENT_PEN);
 
341
        capdc.SetBrush(*wxWHITE_BRUSH);
 
342
        capdc.DrawRectangle(0, 0, resx, resy);
 
343
}
 
344
 
 
345
BEGIN_EVENT_TABLE(ColorPickerScreenDropper, wxControl)
 
346
        EVT_PAINT(ColorPickerScreenDropper::OnPaint)
 
347
        EVT_MOUSE_EVENTS(ColorPickerScreenDropper::OnMouse)
 
348
END_EVENT_TABLE()
 
349
 
 
350
DEFINE_EVENT_TYPE(wxDROPPER_SELECT)
 
351
 
 
352
void ColorPickerScreenDropper::OnMouse(wxMouseEvent &evt)
 
353
{
 
354
        int x, y;
 
355
        x = evt.GetX() / magnification;
 
356
        y = evt.GetY() / magnification;
 
357
 
 
358
        if (HasCapture() && evt.LeftIsDown()) {
 
359
 
 
360
                wxPoint pos = ClientToScreen(evt.GetPosition());
 
361
                DropFromScreenXY(pos.x, pos.y);
 
362
 
 
363
        } else if (evt.LeftDown()) {
 
364
 
 
365
                if (x == 0 && y == 0 && integrated_dropper) {
 
366
                        //SetCursor(*wxCROSS_CURSOR);
 
367
                        CaptureMouse();
 
368
 
 
369
                } else if (x >= 0 && y >= 0 && x < resx && y < resy) {
 
370
                        wxColour color;
 
371
#ifdef __WXMAC__
 
372
                        // wxMemoryDC::GetPixel() isn't implemented on OS X
 
373
                        // Work around it by reading pixel data from the bitmap instead
 
374
                        wxAlphaPixelData cappd(capture);
 
375
                        wxAlphaPixelData::Iterator cappdi(cappd);
 
376
                        cappdi.MoveTo(cappd, x, y);
 
377
                        color.Set(cappdi.Red(), cappdi.Green(), cappdi.Blue());
 
378
#else
 
379
                        wxMemoryDC capdc(capture);
 
380
                        capdc.GetPixel(x, y, &color);
 
381
#endif
 
382
                        color = wxColour(color.Red(), color.Green(), color.Blue(), wxALPHA_OPAQUE);
 
383
                        AssColor ass(color);
 
384
                        wxCommandEvent evt(wxDROPPER_SELECT, GetId());
 
385
                        evt.SetString(ass.GetASSFormatted(false, false, false));
 
386
                        AddPendingEvent(evt);
 
387
                }
 
388
 
 
389
        } else if (HasCapture() && evt.LeftUp()) {
 
390
                ReleaseMouse();
 
391
                //SetCursor(wxNullCursor);
 
392
        }
 
393
}
 
394
 
 
395
void ColorPickerScreenDropper::OnPaint(wxPaintEvent &evt)
 
396
{
 
397
        wxPaintDC pdc(this);
 
398
        
 
399
#ifdef __WXMAC__
 
400
        // See OnMouse() above
 
401
        wxAlphaPixelData cappd(capture);
 
402
        wxAlphaPixelData::Iterator cappdi(cappd);
 
403
#else
 
404
        wxMemoryDC capdc(capture);
 
405
#endif
 
406
 
 
407
        pdc.SetPen(*wxTRANSPARENT_PEN);
 
408
 
 
409
        for (int x = 0; x < resx; x++) {
 
410
                for (int y = 0; y < resy; y++) {
 
411
                        if (x==0 && y==0 && integrated_dropper) continue;
 
412
 
 
413
                        wxColour color;
 
414
#ifdef __WXMAC__
 
415
                        cappdi.MoveTo(cappd, x, y);
 
416
                        color.Set(cappdi.Red(), cappdi.Green(), cappdi.Blue());
 
417
#else
 
418
                        capdc.GetPixel(x, y, &color);
 
419
#endif
 
420
                        pdc.SetBrush(wxBrush(color));
 
421
 
 
422
                        pdc.DrawRectangle(x*magnification, y*magnification, magnification, magnification);
 
423
                }
 
424
        }
 
425
 
 
426
        if (integrated_dropper) {
 
427
                wxBrush cbrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
 
428
                pdc.SetBrush(cbrush);
 
429
                pdc.DrawRectangle(0, 0, magnification, magnification);
 
430
                cbrush.SetStyle(wxCROSSDIAG_HATCH);
 
431
                cbrush.SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
 
432
                pdc.SetBrush(cbrush);
 
433
                pdc.DrawRectangle(0, 0, magnification, magnification);
 
434
        }
 
435
}
 
436
 
 
437
 
 
438
 
 
439
void ColorPickerScreenDropper::DropFromScreenXY(int x, int y)
 
440
{
 
441
        wxMemoryDC capdc(capture);
 
442
        wxScreenDC screen;
 
443
 
 
444
#ifdef __WXMAC__
 
445
        wxBitmap screenbmp = screen.GetAsBitmap().GetSubBitmap(wxRect(x-resx/2, y-resy/2, resx, resy));
 
446
        capdc.DrawBitmap(screenbmp, 0, 0);
 
447
#else
 
448
        screen.StartDrawingOnTop();
 
449
        capdc.Blit(0, 0, resx, resy, &screen, x-resx/2, y-resy/2);
 
450
        screen.EndDrawingOnTop();
 
451
#endif
 
452
 
 
453
        Refresh(false);
 
454
}
 
455
 
 
456
 
 
457
 
 
458
wxColour GetColorFromUser(wxWindow *parent, wxColour original)
 
459
{
 
460
        DialogColorPicker dialog(parent, original);
 
461
        if (dialog.ShowModal() == wxID_OK) {
 
462
                return dialog.GetColor();
 
463
        } else {
 
464
                return original;
 
465
        }
 
466
}
 
467
 
 
468
 
 
469
// Constructor
 
470
DialogColorPicker::DialogColorPicker(wxWindow *parent, wxColour initial_color)
 
471
: wxDialog(parent, -1, _("Select Colour"), wxDefaultPosition, wxDefaultSize)
 
472
{
 
473
        rgb_spectrum[0] =
 
474
        rgb_spectrum[1] =
 
475
        rgb_spectrum[2] =
 
476
        hsl_spectrum    =
 
477
        hsv_spectrum    = 0;
 
478
        spectrum_dirty = true;
 
479
 
 
480
        // generate spectrum slider bar images
 
481
        wxImage sliderimg(slider_width, 256, true);
 
482
        unsigned char *oslid, *slid;
 
483
 
 
484
        // red
 
485
        oslid = slid = (unsigned char *)malloc(slider_width*256*3);
 
486
        for (int  y = 0; y < 256; y++) {
 
487
                for (int x = 0; x < slider_width; x++) {
 
488
                        *slid++ = clip_colorval(y);
 
489
                        *slid++ = 0;
 
490
                        *slid++ = 0;
 
491
                }
 
492
        }
 
493
        sliderimg.SetData(oslid);
 
494
        rgb_slider[0] = new wxBitmap(sliderimg);
 
495
 
 
496
        // green
 
497
        oslid = slid = (unsigned char *)malloc(slider_width*256*3);
 
498
        for (int y = 0; y < 256; y++) {
 
499
                for (int x = 0; x < slider_width; x++) {
 
500
                        *slid++ = 0;
 
501
                        *slid++ = clip_colorval(y);
 
502
                        *slid++ = 0;
 
503
                }
 
504
        }
 
505
        sliderimg.SetData(oslid);
 
506
        rgb_slider[1] = new wxBitmap(sliderimg);
 
507
 
 
508
        // blue
 
509
        oslid = slid = (unsigned char *)malloc(slider_width*256*3);
 
510
        for (int y = 0; y < 256; y++) {
 
511
                for (int x = 0; x < slider_width; x++) {
 
512
                        *slid++ = 0;
 
513
                        *slid++ = 0;
 
514
                        *slid++ = clip_colorval(y);
 
515
                }
 
516
        }
 
517
        sliderimg.SetData(oslid);
 
518
        rgb_slider[2] = new wxBitmap(sliderimg);
 
519
 
 
520
        // luminance
 
521
        oslid = slid = (unsigned char *)malloc(slider_width*256*3);
 
522
        for (int y = 0; y < 256; y++) {
 
523
                int x = 0;
 
524
                for (; x < slider_width; x++) {
 
525
                        *slid++ = clip_colorval(y);
 
526
                        *slid++ = clip_colorval(y);
 
527
                        *slid++ = clip_colorval(y);
 
528
                }
 
529
        }
 
530
        sliderimg.SetData(oslid);
 
531
        hsl_slider = new wxBitmap(sliderimg);
 
532
 
 
533
        oslid = slid = (unsigned char *)malloc(slider_width*256*3);
 
534
        for (int y = 0; y < 256; y++) {
 
535
                for (int x = 0; x < slider_width; x++) {
 
536
                        hsv_to_rgb(y, 255, 255, slid, slid+1, slid+2);
 
537
                        slid += 3;
 
538
                }
 
539
        }
 
540
        sliderimg.SetData(oslid);
 
541
        hsv_slider = new wxBitmap(sliderimg);
 
542
 
 
543
        // Create the controls for the dialog
 
544
        wxSizer *spectrum_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Colour spectrum"));
 
545
        spectrum = new ColorPickerSpectrum(this, SELECTOR_SPECTRUM, 0, -1, -1, ColorPickerSpectrum::HorzVert, wxSize(256, 256));
 
546
        slider = new ColorPickerSpectrum(this, SELECTOR_SLIDER, 0, -1, -1, ColorPickerSpectrum::Vert, wxSize(slider_width, 256));
 
547
        wxString modes[] = { _("RGB/R"), _("RGB/G"), _("RGB/B"), _("HSL/L"), _("HSV/H") };
 
548
        colorspace_choice = new wxChoice(this, SELECTOR_MODE, wxDefaultPosition, wxDefaultSize, 5, modes);
 
549
 
 
550
        wxSize colorinput_size(70, -1);
 
551
        wxSize colorinput_labelsize(40, -1);
 
552
 
 
553
        wxSizer *rgb_box = new wxStaticBoxSizer(wxHORIZONTAL, this, _("RGB colour"));
 
554
        rgb_input[0] = new wxSpinCtrl(this, SELECTOR_RGB_R, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
555
        rgb_input[1] = new wxSpinCtrl(this, SELECTOR_RGB_G, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
556
        rgb_input[2] = new wxSpinCtrl(this, SELECTOR_RGB_B, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
557
 
 
558
        wxSizer *hsl_box = new wxStaticBoxSizer(wxVERTICAL, this, _("HSL colour"));
 
559
        hsl_input[0] = new wxSpinCtrl(this, SELECTOR_HSL_H, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
560
        hsl_input[1] = new wxSpinCtrl(this, SELECTOR_HSL_S, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
561
        hsl_input[2] = new wxSpinCtrl(this, SELECTOR_HSL_L, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
562
 
 
563
        wxSizer *hsv_box = new wxStaticBoxSizer(wxVERTICAL, this, _("HSV colour"));
 
564
        hsv_input[0] = new wxSpinCtrl(this, SELECTOR_HSV_H, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
565
        hsv_input[1] = new wxSpinCtrl(this, SELECTOR_HSV_S, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
566
        hsv_input[2] = new wxSpinCtrl(this, SELECTOR_HSV_V, _T(""), wxDefaultPosition, colorinput_size, wxSP_ARROW_KEYS, 0, 255);
 
567
 
 
568
        ass_input = new wxTextCtrl(this, SELECTOR_ASS_INPUT, _T(""), wxDefaultPosition, colorinput_size);
 
569
        html_input = new wxTextCtrl(this, SELECTOR_HTML_INPUT, _T(""), wxDefaultPosition, colorinput_size);
 
570
 
 
571
        preview_bitmap = wxBitmap(40, 40, 24);
 
572
        preview_box = new wxStaticBitmap(this, -1, preview_bitmap, wxDefaultPosition, wxSize(40, 40), STATIC_BORDER_FLAG);
 
573
 
 
574
        recent_box = new ColorPickerRecent(this, SELECTOR_RECENT, 8, 4, 16);
 
575
 
 
576
        eyedropper_bitmap = wxBITMAP(eyedropper_tool);
 
577
        eyedropper_bitmap.SetMask(new wxMask(eyedropper_bitmap, wxColour(255, 0, 255)));
 
578
        screen_dropper_icon = new wxStaticBitmap(this, SELECTOR_DROPPER, eyedropper_bitmap, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER);
 
579
        screen_dropper = new ColorPickerScreenDropper(this, SELECTOR_DROPPER_PICK, 7, 7, 8, false);
 
580
 
 
581
        // Arrange the controls in a nice way
 
582
        wxSizer *spectop_sizer = new wxBoxSizer(wxHORIZONTAL);
 
583
        spectop_sizer->Add(new wxStaticText(this, -1, _("Spectrum mode:")), 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxRIGHT, 5);
 
584
        spectop_sizer->Add(colorspace_choice, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT);
 
585
        spectop_sizer->Add(5, 5, 1, wxEXPAND);
 
586
        spectop_sizer->Add(preview_box, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT);
 
587
        wxSizer *spectrum_sizer = new wxFlexGridSizer(2, 5, 5);
 
588
        spectrum_sizer->Add(spectop_sizer, wxEXPAND);
 
589
        spectrum_sizer->AddStretchSpacer(1);
 
590
        spectrum_sizer->Add(spectrum);
 
591
        spectrum_sizer->Add(slider);
 
592
        spectrum_box->Add(spectrum_sizer, 0, wxALL, 3);
 
593
 
 
594
        wxFlexGridSizer *rgb_sizer = new wxFlexGridSizer(2, 5, 5);
 
595
        rgb_sizer->Add(new wxStaticText(this, -1, _("Red:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
596
        rgb_sizer->Add(rgb_input[0], 0);
 
597
        rgb_sizer->Add(new wxStaticText(this, -1, _("Green:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
598
        rgb_sizer->Add(rgb_input[1], 0);
 
599
        rgb_sizer->Add(new wxStaticText(this, -1, _("Blue:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
600
        rgb_sizer->Add(rgb_input[2], 0);
 
601
        rgb_sizer->AddGrowableCol(0,1);
 
602
        rgb_box->Add(rgb_sizer, 1, wxEXPAND | wxALL, 3);
 
603
 
 
604
        wxFlexGridSizer *ass_input_sizer = new wxFlexGridSizer(2, 5, 5);
 
605
        ass_input_sizer->Add(new wxStaticText(this, -1, _T("ASS:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
606
        ass_input_sizer->Add(ass_input, 0);
 
607
        ass_input_sizer->Add(new wxStaticText(this, -1, _T("HTML:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
608
        ass_input_sizer->Add(html_input, 0);
 
609
        ass_input_sizer->AddStretchSpacer();
 
610
        ass_input_sizer->AddGrowableCol(0,1);
 
611
        rgb_box->Add(ass_input_sizer, 0, wxALL|wxCENTER|wxEXPAND, 3);
 
612
 
 
613
        wxFlexGridSizer *hsl_sizer = new wxFlexGridSizer(2, 5, 5);
 
614
        hsl_sizer->Add(new wxStaticText(this, -1, _("Hue:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
615
        hsl_sizer->Add(hsl_input[0], 0);
 
616
        hsl_sizer->Add(new wxStaticText(this, -1, _("Sat.:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
617
        hsl_sizer->Add(hsl_input[1], 0);
 
618
        hsl_sizer->Add(new wxStaticText(this, -1, _("Lum.:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
619
        hsl_sizer->Add(hsl_input[2], 0);
 
620
        hsl_sizer->AddGrowableCol(0,1);
 
621
        hsl_box->Add(hsl_sizer, 0, wxALL|wxEXPAND, 3);
 
622
 
 
623
        wxFlexGridSizer *hsv_sizer = new wxFlexGridSizer(2, 5, 5);
 
624
        hsv_sizer->Add(new wxStaticText(this, -1, _("Hue:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
625
        hsv_sizer->Add(hsv_input[0], 0);
 
626
        hsv_sizer->Add(new wxStaticText(this, -1, _("Sat.:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
627
        hsv_sizer->Add(hsv_input[1], 0);
 
628
        hsv_sizer->Add(new wxStaticText(this, -1, _("Value:"), wxDefaultPosition, colorinput_labelsize), 1, wxALIGN_CENTER_VERTICAL|wxEXPAND);
 
629
        hsv_sizer->Add(hsv_input[2], 0);
 
630
        hsv_sizer->AddGrowableCol(0,1);
 
631
        hsv_box->Add(hsv_sizer, 0, wxALL|wxEXPAND, 3);
 
632
 
 
633
        wxSizer *hsx_sizer = new wxBoxSizer(wxHORIZONTAL);
 
634
        hsx_sizer->Add(hsl_box);
 
635
        hsx_sizer->AddSpacer(5);
 
636
        hsx_sizer->Add(hsv_box);
 
637
 
 
638
        wxSizer *recent_sizer = new wxBoxSizer(wxVERTICAL);
 
639
        recent_sizer->Add(recent_box, 1, wxEXPAND);
 
640
        if (Options.AsBool(_T("RGBAdjust Tool"))) recent_sizer->Add(new wxButton(this,BUTTON_RGBADJUST,_T("rgbadjust()")), 0, wxEXPAND);
 
641
 
 
642
        wxSizer *picker_sizer = new wxBoxSizer(wxHORIZONTAL);
 
643
        picker_sizer->AddStretchSpacer();
 
644
        picker_sizer->Add(screen_dropper_icon, 0, wxALIGN_CENTER|wxRIGHT, 5);
 
645
        picker_sizer->Add(screen_dropper, 0, wxALIGN_CENTER);
 
646
        picker_sizer->AddStretchSpacer();
 
647
        picker_sizer->Add(recent_sizer, 0, wxALIGN_CENTER);
 
648
        picker_sizer->AddStretchSpacer();
 
649
 
 
650
        wxStdDialogButtonSizer *button_sizer = new wxStdDialogButtonSizer();
 
651
        button_sizer->AddButton(new wxButton(this,wxID_OK));
 
652
        button_sizer->AddButton(new wxButton(this,wxID_CANCEL));
 
653
        button_sizer->AddButton(new HelpButton(this,_("Colour Picker")));
 
654
        button_sizer->Realize();
 
655
 
 
656
        wxSizer *input_sizer = new wxBoxSizer(wxVERTICAL);
 
657
        input_sizer->Add(rgb_box, 0, wxALIGN_CENTER|wxEXPAND);
 
658
        input_sizer->AddSpacer(5);
 
659
        input_sizer->Add(hsx_sizer, 0, wxALIGN_CENTER|wxEXPAND);
 
660
        input_sizer->AddStretchSpacer(1);
 
661
        input_sizer->Add(picker_sizer, 0, wxALIGN_CENTER|wxEXPAND);
 
662
        input_sizer->AddStretchSpacer(2);
 
663
        input_sizer->Add(button_sizer, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM);
 
664
 
 
665
        wxSizer *main_sizer = new wxBoxSizer(wxHORIZONTAL);
 
666
        main_sizer->Add(spectrum_box, 1, wxALL | wxEXPAND, 5);
 
667
        main_sizer->Add(input_sizer, 0, (wxALL&~wxLEFT)|wxEXPAND, 5);
 
668
 
 
669
        SetSizer(main_sizer);
 
670
        main_sizer->SetSizeHints(this);
 
671
 
 
672
        ass_input->SetFocus();
 
673
 
 
674
        // Position window
 
675
        if (lastx == -1 && lasty == -1) {
 
676
                CenterOnParent();
 
677
        } else {
 
678
                Move(lastx, lasty);
 
679
        }
 
680
 
 
681
        // Fill the controls
 
682
        updating_controls = false;
 
683
        int mode = Options.AsInt(_T("Color Picker Mode"));
 
684
        if (mode < 0 || mode > 4) mode = 3; // HSL default
 
685
        colorspace_choice->SetSelection(mode);
 
686
        SetColor(initial_color);
 
687
        recent_box->LoadFromString(Options.AsText(_T("Color Picker Recent")));
 
688
 
 
689
        // The mouse event handler for the Dropper control must be manually assigned
 
690
        // The EVT_MOUSE_EVENTS macro can't take a control id
 
691
        screen_dropper_icon->Connect(wxEVT_MOTION, wxMouseEventHandler(DialogColorPicker::OnDropperMouse), 0, this);
 
692
        screen_dropper_icon->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(DialogColorPicker::OnDropperMouse), 0, this);
 
693
        screen_dropper_icon->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(DialogColorPicker::OnDropperMouse), 0, this);
 
694
}
 
695
 
 
696
 
 
697
// Destructor
 
698
DialogColorPicker::~DialogColorPicker()
 
699
{
 
700
        GetPosition(&lastx, &lasty);
 
701
 
 
702
        delete rgb_spectrum[0];
 
703
        delete rgb_spectrum[1];
 
704
        delete rgb_spectrum[2];
 
705
        delete hsl_spectrum;
 
706
        delete hsv_spectrum;
 
707
        delete rgb_slider[0];
 
708
        delete rgb_slider[1];
 
709
        delete rgb_slider[2];
 
710
        delete hsl_slider;
 
711
        delete hsv_slider;
 
712
 
 
713
        if (screen_dropper_icon->HasCapture()) screen_dropper_icon->ReleaseMouse();
 
714
}
 
715
 
 
716
 
 
717
// Sets the currently selected color, and updates all controls
 
718
void DialogColorPicker::SetColor(wxColour new_color)
 
719
{
 
720
        cur_color = new_color;
 
721
        rgb_input[0]->SetValue(new_color.Red());
 
722
        rgb_input[1]->SetValue(new_color.Green());
 
723
        rgb_input[2]->SetValue(new_color.Blue());
 
724
        UpdateFromRGB();
 
725
}
 
726
 
 
727
 
 
728
// Get the currently selected color
 
729
wxColour DialogColorPicker::GetColor()
 
730
{
 
731
        recent_box->AddColor(cur_color);
 
732
        Options.SetText(_T("Color Picker Recent"), recent_box->StoreToString());
 
733
        Options.Save();
 
734
        return cur_color;
 
735
}
 
736
 
 
737
 
 
738
// Use the values entered in the RGB controls to update the other controls
 
739
void DialogColorPicker::UpdateFromRGB()
 
740
{
 
741
        if (updating_controls) return;
 
742
        updating_controls = true;
 
743
 
 
744
        unsigned char r, g, b, h, s, l, h2, s2, v2;
 
745
        r = rgb_input[0]->GetValue();
 
746
        g = rgb_input[1]->GetValue();
 
747
        b = rgb_input[2]->GetValue();
 
748
        rgb_to_hsl(r, g, b, &h, &s, &l);
 
749
        rgb_to_hsv(r, g, b, &h2, &s2, &v2);
 
750
        hsl_input[0]->SetValue(h);
 
751
        hsl_input[1]->SetValue(s);
 
752
        hsl_input[2]->SetValue(l);
 
753
        hsv_input[0]->SetValue(h2);
 
754
        hsv_input[1]->SetValue(s2);
 
755
        hsv_input[2]->SetValue(v2);
 
756
        cur_color = wxColour(r, g, b, wxALPHA_OPAQUE);
 
757
        ass_input->SetValue(AssColor(cur_color).GetASSFormatted(false, false, false));
 
758
        html_input->SetValue(color_to_html(cur_color));
 
759
        UpdateSpectrumDisplay();
 
760
 
 
761
        updating_controls = false;
 
762
}
 
763
 
 
764
 
 
765
// Use the values entered in the HSL controls to update the other controls
 
766
void DialogColorPicker::UpdateFromHSL()
 
767
{
 
768
        if (updating_controls) return;
 
769
        updating_controls = true;
 
770
 
 
771
        unsigned char r, g, b, h, s, l, h2, s2, v2;
 
772
        h = hsl_input[0]->GetValue();
 
773
        s = hsl_input[1]->GetValue();
 
774
        l = hsl_input[2]->GetValue();
 
775
        hsl_to_rgb(h, s, l, &r, &g, &b);
 
776
        hsl_to_hsv(h, s, l, &h2, &s2, &v2);
 
777
        rgb_input[0]->SetValue(r);
 
778
        rgb_input[1]->SetValue(g);
 
779
        rgb_input[2]->SetValue(b);
 
780
        hsv_input[0]->SetValue(h2);
 
781
        hsv_input[1]->SetValue(s2);
 
782
        hsv_input[2]->SetValue(v2);
 
783
        cur_color = wxColour(r, g, b, wxALPHA_OPAQUE);
 
784
        ass_input->SetValue(AssColor(cur_color).GetASSFormatted(false, false, false));
 
785
        html_input->SetValue(color_to_html(cur_color));
 
786
        UpdateSpectrumDisplay();
 
787
 
 
788
        updating_controls = false;
 
789
}
 
790
 
 
791
 
 
792
void DialogColorPicker::UpdateFromHSV()
 
793
{
 
794
        if (updating_controls) return;
 
795
        updating_controls = true;
 
796
 
 
797
        unsigned char r, g, b, h, s, l, h2, s2, v2;
 
798
        //int r, g, b, h2, s2, v2;
 
799
        h2 = hsv_input[0]->GetValue();
 
800
        s2 = hsv_input[1]->GetValue();
 
801
        v2 = hsv_input[2]->GetValue();
 
802
        hsv_to_rgb(h2, s2, v2, &r, &g, &b);
 
803
        hsv_to_hsl(h2, s2, v2, &h, &s, &l);
 
804
        rgb_input[0]->SetValue(r);
 
805
        rgb_input[1]->SetValue(g);
 
806
        rgb_input[2]->SetValue(b);
 
807
        hsl_input[0]->SetValue(h);
 
808
        hsl_input[1]->SetValue(s);
 
809
        hsl_input[2]->SetValue(l);
 
810
        cur_color = wxColour(r, g, b, wxALPHA_OPAQUE);
 
811
        ass_input->SetValue(AssColor(cur_color).GetASSFormatted(false, false, false));
 
812
        html_input->SetValue(color_to_html(cur_color));
 
813
        UpdateSpectrumDisplay();
 
814
 
 
815
        updating_controls = false;
 
816
}
 
817
 
 
818
 
 
819
// Use the value entered in the ASS hex control to update the other controls
 
820
void DialogColorPicker::UpdateFromASS()
 
821
{
 
822
        if (updating_controls) return;
 
823
        updating_controls = true;
 
824
 
 
825
        unsigned char r, g, b, h, s, l, h2, s2, v2;
 
826
        AssColor ass;
 
827
        ass.Parse(ass_input->GetValue());
 
828
        r = ass.r;
 
829
        g = ass.g;
 
830
        b = ass.b;
 
831
        rgb_to_hsl(r, g, b, &h, &s, &l);
 
832
        rgb_to_hsv(r, g, b, &h2, &s2, &v2);
 
833
        rgb_input[0]->SetValue(r);
 
834
        rgb_input[1]->SetValue(g);
 
835
        rgb_input[2]->SetValue(b);
 
836
        hsl_input[0]->SetValue(h);
 
837
        hsl_input[1]->SetValue(s);
 
838
        hsl_input[2]->SetValue(l);
 
839
        hsv_input[0]->SetValue(h2);
 
840
        hsv_input[1]->SetValue(s2);
 
841
        hsv_input[2]->SetValue(v2);
 
842
        cur_color = wxColour(r, g, b, wxALPHA_OPAQUE);
 
843
        html_input->SetValue(color_to_html(cur_color));
 
844
        UpdateSpectrumDisplay();
 
845
 
 
846
        updating_controls = false;
 
847
}
 
848
 
 
849
 
 
850
void DialogColorPicker::UpdateFromHTML()
 
851
{
 
852
        if (updating_controls) return;
 
853
        updating_controls = true;
 
854
 
 
855
        unsigned char r, g, b, h, s, l, h2, s2, v2;
 
856
        cur_color = html_to_color(html_input->GetValue());
 
857
        r = cur_color.Red();
 
858
        g = cur_color.Green();
 
859
        b = cur_color.Blue();
 
860
        rgb_to_hsl(r, g, b, &h, &s, &l);
 
861
        rgb_to_hsv(r, g, b, &h2, &s2, &v2);
 
862
        rgb_input[0]->SetValue(r);
 
863
        rgb_input[1]->SetValue(g);
 
864
        rgb_input[2]->SetValue(b);
 
865
        hsl_input[0]->SetValue(h);
 
866
        hsl_input[1]->SetValue(s);
 
867
        hsl_input[2]->SetValue(l);
 
868
        hsv_input[0]->SetValue(h2);
 
869
        hsv_input[1]->SetValue(s2);
 
870
        hsv_input[2]->SetValue(v2);
 
871
        cur_color = wxColour(r, g, b, wxALPHA_OPAQUE);
 
872
        ass_input->SetValue(AssColor(cur_color).GetASSFormatted(false, false, false));
 
873
        UpdateSpectrumDisplay();
 
874
 
 
875
        updating_controls = false;
 
876
}
 
877
 
 
878
 
 
879
void DialogColorPicker::UpdateSpectrumDisplay()
 
880
{
 
881
        int i = colorspace_choice->GetSelection();
 
882
        switch (i) {
 
883
                case 0:
 
884
                        if (spectrum_dirty)
 
885
                                spectrum->SetBackground(MakeGBSpectrum());
 
886
                        slider->SetBackground(rgb_slider[0]);
 
887
                        slider->SetXY(0, rgb_input[0]->GetValue());
 
888
                        spectrum->SetXY(rgb_input[2]->GetValue(), rgb_input[1]->GetValue());
 
889
                        break;
 
890
                case 1:
 
891
                        if (spectrum_dirty)
 
892
                                spectrum->SetBackground(MakeRBSpectrum());
 
893
                        slider->SetBackground(rgb_slider[1]);
 
894
                        slider->SetXY(0, rgb_input[1]->GetValue());
 
895
                        spectrum->SetXY(rgb_input[2]->GetValue(), rgb_input[0]->GetValue());
 
896
                        break;
 
897
                case 2:
 
898
                        if (spectrum_dirty)
 
899
                                spectrum->SetBackground(MakeRGSpectrum());
 
900
                        slider->SetBackground(rgb_slider[2]);
 
901
                        slider->SetXY(0, rgb_input[2]->GetValue());
 
902
                        spectrum->SetXY(rgb_input[1]->GetValue(), rgb_input[0]->GetValue());
 
903
                        break;
 
904
                case 3:
 
905
                        if (spectrum_dirty)
 
906
                                spectrum->SetBackground(MakeHSSpectrum());
 
907
                        slider->SetBackground(hsl_slider);
 
908
                        slider->SetXY(0, hsl_input[2]->GetValue());
 
909
                        spectrum->SetXY(hsl_input[1]->GetValue(), hsl_input[0]->GetValue());
 
910
                        break;
 
911
                case 4:
 
912
                        if (spectrum_dirty)
 
913
                                spectrum->SetBackground(MakeSVSpectrum());
 
914
                        slider->SetBackground(hsv_slider);
 
915
                        slider->SetXY(0, hsv_input[0]->GetValue());
 
916
                        spectrum->SetXY(hsv_input[1]->GetValue(), hsv_input[2]->GetValue());
 
917
                        break;
 
918
        }
 
919
        spectrum_dirty = false;
 
920
 
 
921
        wxBitmap tempBmp = preview_box->GetBitmap();
 
922
        {
 
923
                wxMemoryDC previewdc;
 
924
                previewdc.SelectObject(tempBmp);
 
925
                previewdc.SetPen(*wxTRANSPARENT_PEN);
 
926
                previewdc.SetBrush(wxBrush(cur_color));
 
927
                previewdc.DrawRectangle(0, 0, 40, 40);
 
928
        }
 
929
        preview_box->SetBitmap(tempBmp);
 
930
}
 
931
 
 
932
 
 
933
wxBitmap *DialogColorPicker::MakeGBSpectrum()
 
934
{
 
935
        if (rgb_spectrum[0]) delete rgb_spectrum[0];
 
936
 
 
937
        wxImage spectrum_image(256, 256, false);
 
938
        unsigned char *ospec, *spec;
 
939
 
 
940
        ospec = spec = (unsigned char *)malloc(256*256*3);
 
941
        for (int g = 0; g < 256; g++) {
 
942
                for (int b = 0; b < 256; b++) {
 
943
                        *spec++ = cur_color.Red();
 
944
                        *spec++ = g;
 
945
                        *spec++ = b;
 
946
                }
 
947
        }
 
948
        spectrum_image.SetData(ospec);
 
949
        rgb_spectrum[0] = new wxBitmap(spectrum_image);
 
950
 
 
951
        return rgb_spectrum[0];
 
952
}
 
953
 
 
954
 
 
955
wxBitmap *DialogColorPicker::MakeRBSpectrum()
 
956
{
 
957
        if (rgb_spectrum[1]) delete rgb_spectrum[1];
 
958
 
 
959
        wxImage spectrum_image(256, 256, false);
 
960
        unsigned char *ospec, *spec;
 
961
 
 
962
        ospec = spec = (unsigned char *)malloc(256*256*3);
 
963
        for (int r = 0; r < 256; r++) {
 
964
                for (int b = 0; b < 256; b++) {
 
965
                        *spec++ = r;
 
966
                        *spec++ = cur_color.Green();
 
967
                        *spec++ = b;
 
968
                }
 
969
        }
 
970
        spectrum_image.SetData(ospec);
 
971
        rgb_spectrum[1] = new wxBitmap(spectrum_image);
 
972
 
 
973
        return rgb_spectrum[1];
 
974
}
 
975
 
 
976
 
 
977
wxBitmap *DialogColorPicker::MakeRGSpectrum()
 
978
{
 
979
        if (rgb_spectrum[2]) delete rgb_spectrum[2];
 
980
 
 
981
        wxImage spectrum_image(256, 256, false);
 
982
        unsigned char *ospec, *spec;
 
983
 
 
984
        ospec = spec = (unsigned char *)malloc(256*256*3);
 
985
        for (int r = 0; r < 256; r++) {
 
986
                for (int g = 0; g < 256; g++) {
 
987
                        *spec++ = r;
 
988
                        *spec++ = g;
 
989
                        *spec++ = cur_color.Blue();
 
990
                }
 
991
        }
 
992
        spectrum_image.SetData(ospec);
 
993
        rgb_spectrum[2] = new wxBitmap(spectrum_image);
 
994
 
 
995
        return rgb_spectrum[2];
 
996
}
 
997
 
 
998
 
 
999
wxBitmap *DialogColorPicker::MakeHSSpectrum()
 
1000
{
 
1001
        if (hsl_spectrum) delete hsl_spectrum;
 
1002
 
 
1003
        wxImage spectrum_image(256, 256, false);
 
1004
        unsigned char *ospec, *spec;
 
1005
 
 
1006
        ospec = spec = (unsigned char *)malloc(256*256*3);
 
1007
        int l = hsl_input[2]->GetValue();
 
1008
 
 
1009
        for (int h = 0; h < 256; h++) {
 
1010
                unsigned char maxr, maxg, maxb;
 
1011
                hsl_to_rgb(h, 255, l, &maxr, &maxg, &maxb);
 
1012
 
 
1013
                for (int s = 0; s < 256; s++) {
 
1014
                        *spec++ = maxr * s / 256 + (255-s) * l / 256;
 
1015
                        *spec++ = maxg * s / 256 + (255-s) * l / 256;
 
1016
                        *spec++ = maxb * s / 256 + (255-s) * l / 256;
 
1017
                }
 
1018
        }
 
1019
        spectrum_image.SetData(ospec);
 
1020
        hsl_spectrum = new wxBitmap(spectrum_image);
 
1021
 
 
1022
        return hsl_spectrum;
 
1023
}
 
1024
 
 
1025
 
 
1026
wxBitmap *DialogColorPicker::MakeSVSpectrum()
 
1027
{
 
1028
        if (hsv_spectrum) delete hsv_spectrum;
 
1029
 
 
1030
        wxImage spectrum_image(256, 256, false);
 
1031
        unsigned char *ospec, *spec;
 
1032
 
 
1033
        ospec = spec = (unsigned char *)malloc(256*256*3);
 
1034
 
 
1035
        int h = hsv_input[0]->GetValue();
 
1036
        unsigned char maxr, maxg, maxb;
 
1037
        hsv_to_rgb(h, 255, 255, &maxr, &maxg, &maxb);
 
1038
 
 
1039
        for (int v = 0; v < 256; v++) {
 
1040
                int rr, rg, rb;
 
1041
                rr = (255-maxr) * v / 256;
 
1042
                rg = (255-maxg) * v / 256;
 
1043
                rb = (255-maxb) * v / 256;
 
1044
                for (int s = 0; s < 256; s++) {
 
1045
                        int r, g, b;
 
1046
                        r = 255 - rr * s / 256 - (255-v);
 
1047
                        g = 255 - rg * s / 256 - (255-v);
 
1048
                        b = 255 - rb * s / 256 - (255-v);
 
1049
                        *spec++ = r;
 
1050
                        *spec++ = g;
 
1051
                        *spec++ = b;
 
1052
                }
 
1053
        }
 
1054
        spectrum_image.SetData(ospec);
 
1055
        hsv_spectrum = new wxBitmap(spectrum_image);
 
1056
 
 
1057
        return hsv_spectrum;
 
1058
}
 
1059
 
 
1060
 
 
1061
 
 
1062
BEGIN_EVENT_TABLE(DialogColorPicker, wxDialog)
 
1063
        EVT_SPINCTRL(SELECTOR_RGB_R, DialogColorPicker::OnSpinRGB)
 
1064
        EVT_SPINCTRL(SELECTOR_RGB_G, DialogColorPicker::OnSpinRGB)
 
1065
        EVT_SPINCTRL(SELECTOR_RGB_B, DialogColorPicker::OnSpinRGB)
 
1066
        EVT_SPINCTRL(SELECTOR_HSL_H, DialogColorPicker::OnSpinHSL)
 
1067
        EVT_SPINCTRL(SELECTOR_HSL_S, DialogColorPicker::OnSpinHSL)
 
1068
        EVT_SPINCTRL(SELECTOR_HSL_L, DialogColorPicker::OnSpinHSL)
 
1069
        EVT_SPINCTRL(SELECTOR_HSV_H, DialogColorPicker::OnSpinHSV)
 
1070
        EVT_SPINCTRL(SELECTOR_HSV_S, DialogColorPicker::OnSpinHSV)
 
1071
        EVT_SPINCTRL(SELECTOR_HSV_V, DialogColorPicker::OnSpinHSV)
 
1072
        EVT_TEXT(SELECTOR_RGB_R, DialogColorPicker::OnChangeRGB)
 
1073
        EVT_TEXT(SELECTOR_RGB_G, DialogColorPicker::OnChangeRGB)
 
1074
        EVT_TEXT(SELECTOR_RGB_B, DialogColorPicker::OnChangeRGB)
 
1075
        EVT_TEXT(SELECTOR_HSL_H, DialogColorPicker::OnChangeHSL)
 
1076
        EVT_TEXT(SELECTOR_HSL_S, DialogColorPicker::OnChangeHSL)
 
1077
        EVT_TEXT(SELECTOR_HSL_L, DialogColorPicker::OnChangeHSL)
 
1078
        EVT_TEXT(SELECTOR_HSV_H, DialogColorPicker::OnChangeHSV)
 
1079
        EVT_TEXT(SELECTOR_HSV_S, DialogColorPicker::OnChangeHSV)
 
1080
        EVT_TEXT(SELECTOR_HSV_V, DialogColorPicker::OnChangeHSV)
 
1081
        EVT_TEXT(SELECTOR_ASS_INPUT, DialogColorPicker::OnChangeASS)
 
1082
        EVT_TEXT(SELECTOR_HTML_INPUT, DialogColorPicker::OnChangeHTML)
 
1083
        EVT_CHOICE(SELECTOR_MODE, DialogColorPicker::OnChangeMode)
 
1084
        EVT_COMMAND(SELECTOR_SPECTRUM, wxSPECTRUM_CHANGE, DialogColorPicker::OnSpectrumChange)
 
1085
        EVT_COMMAND(SELECTOR_SLIDER, wxSPECTRUM_CHANGE, DialogColorPicker::OnSliderChange)
 
1086
        EVT_COMMAND(SELECTOR_RECENT, wxRECENT_SELECT, DialogColorPicker::OnRecentSelect)
 
1087
        EVT_COMMAND(SELECTOR_DROPPER_PICK, wxDROPPER_SELECT, DialogColorPicker::OnRecentSelect)
 
1088
        EVT_BUTTON(BUTTON_RGBADJUST, DialogColorPicker::OnRGBAdjust)
 
1089
        EVT_MOUSE_EVENTS(DialogColorPicker::OnMouse)
 
1090
END_EVENT_TABLE()
 
1091
 
 
1092
 
 
1093
void DialogColorPicker::OnSpinRGB(wxSpinEvent &evt)
 
1094
{
 
1095
        if (!updating_controls)
 
1096
                spectrum_dirty = true;
 
1097
        UpdateFromRGB();
 
1098
}
 
1099
 
 
1100
 
 
1101
void DialogColorPicker::OnSpinHSL(wxSpinEvent &evt)
 
1102
{
 
1103
        if (!updating_controls)
 
1104
                spectrum_dirty = true;
 
1105
        UpdateFromHSL();
 
1106
}
 
1107
 
 
1108
 
 
1109
void DialogColorPicker::OnSpinHSV(wxSpinEvent &evt)
 
1110
{
 
1111
        if (!updating_controls)
 
1112
                spectrum_dirty = true;
 
1113
        UpdateFromHSV();
 
1114
}
 
1115
 
 
1116
 
 
1117
void DialogColorPicker::OnChangeRGB(wxCommandEvent &evt)
 
1118
{
 
1119
        if (!updating_controls)
 
1120
                spectrum_dirty = true;
 
1121
        UpdateFromRGB();
 
1122
}
 
1123
 
 
1124
 
 
1125
void DialogColorPicker::OnChangeHSL(wxCommandEvent &evt)
 
1126
{
 
1127
        if (!updating_controls)
 
1128
                spectrum_dirty = true;
 
1129
        UpdateFromHSL();
 
1130
}
 
1131
 
 
1132
 
 
1133
void DialogColorPicker::OnChangeHSV(wxCommandEvent &evt)
 
1134
{
 
1135
        if (!updating_controls)
 
1136
                spectrum_dirty = true;
 
1137
        UpdateFromHSV();
 
1138
}
 
1139
 
 
1140
 
 
1141
void DialogColorPicker::OnChangeASS(wxCommandEvent &evt)
 
1142
{
 
1143
        if (!updating_controls)
 
1144
                spectrum_dirty = true;
 
1145
        UpdateFromASS();
 
1146
}
 
1147
 
 
1148
 
 
1149
void DialogColorPicker::OnChangeHTML(wxCommandEvent &evt)
 
1150
{
 
1151
        if (!updating_controls)
 
1152
                spectrum_dirty = true;
 
1153
        UpdateFromHTML();
 
1154
}
 
1155
 
 
1156
 
 
1157
void DialogColorPicker::OnChangeMode(wxCommandEvent &evt)
 
1158
{
 
1159
        if (!updating_controls)
 
1160
                spectrum_dirty = true;
 
1161
        Options.SetInt(_T("Color Picker Mode"), colorspace_choice->GetSelection());
 
1162
        UpdateSpectrumDisplay();
 
1163
}
 
1164
 
 
1165
 
 
1166
void DialogColorPicker::OnSpectrumChange(wxCommandEvent &evt)
 
1167
{
 
1168
        updating_controls = true;
 
1169
 
 
1170
        int i = colorspace_choice->GetSelection();
 
1171
        int x, y;
 
1172
        spectrum->GetXY(x, y);
 
1173
        switch (i) {
 
1174
                case 0:
 
1175
                        rgb_input[2]->SetValue(x);
 
1176
                        rgb_input[1]->SetValue(y);
 
1177
                        updating_controls = false;
 
1178
                        UpdateFromRGB();
 
1179
                        break;
 
1180
                case 1:
 
1181
                        rgb_input[2]->SetValue(x);
 
1182
                        rgb_input[0]->SetValue(y);
 
1183
                        updating_controls = false;
 
1184
                        UpdateFromRGB();
 
1185
                        break;
 
1186
                case 2:
 
1187
                        rgb_input[1]->SetValue(x);
 
1188
                        rgb_input[0]->SetValue(y);
 
1189
                        updating_controls = false;
 
1190
                        UpdateFromRGB();
 
1191
                        break;
 
1192
                case 3:
 
1193
                        hsl_input[1]->SetValue(x);
 
1194
                        hsl_input[0]->SetValue(y);
 
1195
                        updating_controls = false;
 
1196
                        UpdateFromHSL();
 
1197
                        break;
 
1198
                case 4:
 
1199
                        hsv_input[1]->SetValue(x);
 
1200
                        hsv_input[2]->SetValue(y);
 
1201
                        updating_controls = false;
 
1202
                        UpdateFromHSV();
 
1203
                        break;
 
1204
        }
 
1205
 
 
1206
}
 
1207
 
 
1208
 
 
1209
void DialogColorPicker::OnSliderChange(wxCommandEvent &evt)
 
1210
{
 
1211
        spectrum_dirty = true;
 
1212
        int i = colorspace_choice->GetSelection();
 
1213
        int x, y; // only y is used, x is garbage for this control
 
1214
        slider->GetXY(x, y);
 
1215
        switch (i) {
 
1216
                // setting the value of a component input automatically invalidates the spectrum
 
1217
                // and calls the according UpdateFromXXX() function in wxMSW and wxMac, wxGTK must be manually updated 
 
1218
                case 0:
 
1219
                        rgb_input[0]->SetValue(y);
 
1220
#ifdef __WXGTK__
 
1221
                        UpdateFromRGB();
 
1222
#endif
 
1223
                        break;
 
1224
                case 1:
 
1225
                        rgb_input[1]->SetValue(y);
 
1226
#ifdef __WXGTK__
 
1227
                        UpdateFromRGB();
 
1228
#endif
 
1229
                        break;
 
1230
                case 2:
 
1231
                        rgb_input[2]->SetValue(y);
 
1232
#ifdef __WXGTK__
 
1233
                        UpdateFromRGB();
 
1234
#endif
 
1235
                        break;
 
1236
                case 3:
 
1237
                        hsl_input[2]->SetValue(y);
 
1238
#ifdef __WXGTK__
 
1239
                        UpdateFromHSL();
 
1240
#endif
 
1241
                        break;
 
1242
                case 4:
 
1243
                        hsv_input[0]->SetValue(y);
 
1244
#ifdef __WXGTK__
 
1245
                        UpdateFromHSV();
 
1246
#endif
 
1247
                        break;
 
1248
        }
 
1249
}
 
1250
 
 
1251
 
 
1252
void DialogColorPicker::OnRecentSelect(wxCommandEvent &evt)
 
1253
{
 
1254
        // The colour picked is stored in the event string
 
1255
        // Allows this event handler to be shared by recent and dropper controls
 
1256
        // Ugly hack?
 
1257
        AssColor color;
 
1258
        color.Parse(evt.GetString());
 
1259
        SetColor(color.GetWXColor());
 
1260
}
 
1261
 
 
1262
 
 
1263
void DialogColorPicker::OnDropperMouse(wxMouseEvent &evt)
 
1264
{
 
1265
        if (evt.LeftDown() && !screen_dropper_icon->HasCapture()) {
 
1266
#ifdef WIN32
 
1267
                screen_dropper_icon->SetCursor(wxCursor(_T("eyedropper_cursor")));
 
1268
#else
 
1269
                screen_dropper_icon->SetCursor(*wxCROSS_CURSOR);
 
1270
#endif
 
1271
                screen_dropper_icon->SetBitmap(wxNullBitmap);
 
1272
                screen_dropper_icon->CaptureMouse();
 
1273
                eyedropper_grab_point = evt.GetPosition();
 
1274
                eyedropper_is_grabbed = false;
 
1275
        }
 
1276
 
 
1277
        if (evt.LeftUp()) {
 
1278
#define ABS(x) (x < 0 ? -x : x)
 
1279
                wxPoint ptdiff = evt.GetPosition() - eyedropper_grab_point;
 
1280
                bool release_now = eyedropper_is_grabbed || ABS(ptdiff.x) + ABS(ptdiff.y) > 7;
 
1281
                if (release_now) {
 
1282
                        screen_dropper_icon->ReleaseMouse();
 
1283
                        eyedropper_is_grabbed = false;
 
1284
                        screen_dropper_icon->SetCursor(wxNullCursor);
 
1285
                        screen_dropper_icon->SetBitmap(eyedropper_bitmap);
 
1286
                } else {
 
1287
                        eyedropper_is_grabbed = true;
 
1288
                }
 
1289
        }
 
1290
 
 
1291
        if (screen_dropper_icon->HasCapture()) {
 
1292
                wxPoint scrpos = screen_dropper_icon->ClientToScreen(evt.GetPosition());
 
1293
                screen_dropper->DropFromScreenXY(scrpos.x, scrpos.y);
 
1294
        }
 
1295
}
 
1296
 
 
1297
 
 
1298
 
 
1299
/// @brief Hack to redirect events to the screen dropper icon
 
1300
/// @param evt 
 
1301
///
 
1302
void DialogColorPicker::OnMouse(wxMouseEvent &evt)
 
1303
{
 
1304
        if (screen_dropper_icon->HasCapture()) {
 
1305
                wxPoint dropper_pos = screen_dropper_icon->ScreenToClient(ClientToScreen(evt.GetPosition()));
 
1306
                evt.m_x = dropper_pos.x;
 
1307
                evt.m_y = dropper_pos.y;
 
1308
                screen_dropper_icon->GetEventHandler()->ProcessEvent(evt);
 
1309
        }
 
1310
        else
 
1311
                evt.Skip();
 
1312
}
 
1313
 
 
1314
 
 
1315
 
 
1316
// rgbadjust() tool
 
1317
void DialogColorPicker::OnRGBAdjust(wxCommandEvent &evt)
 
1318
{
 
1319
        wxColour cur = cur_color;
 
1320
        wxColour old = recent_box->GetColor(0);
 
1321
        double r = double(cur.Red()) / double(old.Red());
 
1322
        double g = double(cur.Green()) / double(old.Green());
 
1323
        double b = double(cur.Blue()) / double(old.Blue());
 
1324
        wxString data = wxString(_T("rgbadjust(")) + PrettyFloatD(r) + _T(",") + PrettyFloatD(g) + _T(",") + PrettyFloatD(b) + _T(")");
 
1325
 
 
1326
        if (wxTheClipboard->Open())     {
 
1327
                wxTheClipboard->SetData(new wxTextDataObject(data));
 
1328
                wxTheClipboard->Close();
 
1329
        }
 
1330
}
 
1331
 
 
1332
 
 
1333
// Static values for last position of the dialog in this Aegisub session
 
1334
int DialogColorPicker::lastx = -1;
 
1335
int DialogColorPicker::lasty = -1;