forked from mia/Aegisub
Factor out some duplicated code in the color picker slider generation
This commit is contained in:
parent
2113ed9580
commit
2e99223977
1 changed files with 19 additions and 21 deletions
|
@ -525,40 +525,38 @@ static const int slider_width = 10; ///< width in pixels of the color slider con
|
||||||
static const int alpha_box_size = 5;
|
static const int alpha_box_size = 5;
|
||||||
|
|
||||||
template<typename Func>
|
template<typename Func>
|
||||||
static wxBitmap make_slider(Func func) {
|
static wxBitmap make_slider_img(Func func) {
|
||||||
unsigned char *slid = (unsigned char *)calloc(slider_width * 256 * 3, 1);
|
unsigned char *slid = (unsigned char *)calloc(slider_width * 256 * 3, 1);
|
||||||
func(slid);
|
func(slid);
|
||||||
wxImage img(slider_width, 256, slid);
|
wxImage img(slider_width, 256, slid);
|
||||||
return wxBitmap(img);
|
return wxBitmap(img);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Func>
|
||||||
|
static wxBitmap make_slider(Func func) {
|
||||||
|
return make_slider_img([&](unsigned char *slid) {
|
||||||
|
for (int y = 0; y < 256; ++y) {
|
||||||
|
unsigned char rgb[3];
|
||||||
|
func(y, rgb);
|
||||||
|
for (int x = 0; x < slider_width; ++x)
|
||||||
|
memcpy(slid + y * slider_width * 3 + x * 3, rgb, 3);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
DialogColorPicker::DialogColorPicker(wxWindow *parent, agi::Color initial_color, std::function<void (agi::Color)> callback, bool alpha)
|
DialogColorPicker::DialogColorPicker(wxWindow *parent, agi::Color initial_color, std::function<void (agi::Color)> callback, bool alpha)
|
||||||
: wxDialog(parent, -1, _("Select Color"))
|
: wxDialog(parent, -1, _("Select Color"))
|
||||||
, callback(callback)
|
, callback(callback)
|
||||||
{
|
{
|
||||||
// generate spectrum slider bar images
|
// generate spectrum slider bar images
|
||||||
for (int i = 0; i < 3; ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
rgb_slider[i] = make_slider([=](unsigned char *slid) {
|
rgb_slider[i] = make_slider([=](int y, unsigned char *rgb) {
|
||||||
for (int y = 0; y < 256; y++) {
|
memset(rgb, 0, 3);
|
||||||
for (int x = 0; x < slider_width; x++)
|
rgb[i] = y;
|
||||||
slid[y * slider_width + x * 3 + i] = y;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
hsl_slider = make_slider([](int y, unsigned char *rgb) { memset(rgb, y, 3); });
|
||||||
hsl_slider = make_slider([](unsigned char *slid) {
|
hsv_slider = make_slider([](int y, unsigned char *rgb) { hsv_to_rgb(y, 255, 255, rgb, rgb + 1, rgb + 2); });
|
||||||
for (int y = 0; y < 256; ++y)
|
|
||||||
memset(slid + y * slider_width * 3, y, slider_width * 3);
|
|
||||||
});
|
|
||||||
|
|
||||||
hsv_slider = make_slider([](unsigned char *slid) {
|
|
||||||
for (int y = 0; y < 256; ++y) {
|
|
||||||
unsigned char rgb[3];
|
|
||||||
hsv_to_rgb(y, 255, 255, rgb, rgb + 1, rgb + 2);
|
|
||||||
for (int x = 0; x < slider_width; ++x)
|
|
||||||
memcpy(slid + y * slider_width * 3 + x * 3, rgb, 3);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create the controls for the dialog
|
// Create the controls for the dialog
|
||||||
wxSizer *spectrum_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Color spectrum"));
|
wxSizer *spectrum_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Color spectrum"));
|
||||||
|
@ -883,7 +881,7 @@ void DialogColorPicker::UpdateSpectrumDisplay() {
|
||||||
}
|
}
|
||||||
preview_box->SetBitmap(tempBmp);
|
preview_box->SetBitmap(tempBmp);
|
||||||
|
|
||||||
alpha_slider_img = make_slider([=](unsigned char *slid) {
|
alpha_slider_img = make_slider_img([=](unsigned char *slid) {
|
||||||
static_assert(slider_width % alpha_box_size == 0, "Slider width must be a multiple of alpha box width");
|
static_assert(slider_width % alpha_box_size == 0, "Slider width must be a multiple of alpha box width");
|
||||||
|
|
||||||
for (int y = 0; y < 256; ++y) {
|
for (int y = 0; y < 256; ++y) {
|
||||||
|
|
Loading…
Reference in a new issue