Skip to content

Commit

Permalink
Draw alpha slider in ColorSliders correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed May 30, 2017
1 parent 1576e42 commit 629c359
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
44 changes: 39 additions & 5 deletions src/app/modules/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,34 @@
#include "app/modules/palettes.h"
#include "app/ui/editor/editor.h"
#include "app/ui/skin/skin_theme.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "doc/blend_funcs.h"
#include "doc/image.h"
#include "doc/palette.h"
#include "gfx/point.h"
#include "gfx/rect.h"

namespace app {

using namespace app::skin;
using namespace gfx;

namespace {

// TODO hard-coded values, use pref.xml values
gfx::Color gridColor1 = gfx::rgba(128, 128, 128);
gfx::Color gridColor2 = gfx::rgba(192, 192, 192);

} // anonymous namespace

static void rectgrid(ui::Graphics* g, const gfx::Rect& rc, const gfx::Size& tile)
{
if (tile.w < 1 || tile.h < 1)
return;

int x, y, u, v;

// TODO these values are hard-coded in ColorSelector::onPaintAlphaBar() too, use pref.xml values
gfx::Color c1 = gfx::rgba(128, 128, 128);
gfx::Color c2 = gfx::rgba(192, 192, 192);
const gfx::Color c1 = gridColor1;
const gfx::Color c2 = gridColor2;

u = 0;
v = 0;
Expand Down Expand Up @@ -147,4 +155,30 @@ void draw_color_button(ui::Graphics* g,
}
}

void draw_alpha_slider(ui::Graphics* g,
const gfx::Rect& rc,
const app::Color& color)
{
const int xmax = MAX(1, rc.w-1);
const doc::color_t c =
(color.getType() != app::Color::MaskType ?
doc::rgba(color.getRed(),
color.getGreen(),
color.getBlue(), 255): 0);

for (int x=0; x<rc.w; ++x) {
const int a = (255 * x / xmax);
const doc::color_t c1 = doc::rgba_blender_normal(gridColor1, c, a);
const doc::color_t c2 = doc::rgba_blender_normal(gridColor2, c, a);
const int mid = rc.h/2;
const int odd = (x / rc.h) & 1;
g->drawVLine(
app::color_utils::color_for_ui(app::Color::fromImage(IMAGE_RGB, odd ? c2: c1)),
rc.x+x, rc.y, mid);
g->drawVLine(
app::color_utils::color_for_ui(app::Color::fromImage(IMAGE_RGB, odd ? c1: c2)),
rc.x+x, rc.y+mid, rc.h-mid);
}
}

} // namespace app
6 changes: 5 additions & 1 deletion src/app/modules/gfx.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2016 David Capello
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
Expand Down Expand Up @@ -30,6 +30,10 @@ namespace app {
const bool hot,
const bool drag);

void draw_alpha_slider(ui::Graphics* g,
const gfx::Rect& rc,
const app::Color& color);

} // namespace app

#endif
31 changes: 5 additions & 26 deletions src/app/ui/color_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include "app/ui/color_selector.h"

#include "app/color_utils.h"
#include "app/modules/gfx.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui/status_bar.h"
#include "base/scoped_value.h"
#include "doc/blend_funcs.h"
#include "she/surface.h"
#include "ui/manager.h"
#include "ui/message.h"
Expand Down Expand Up @@ -202,32 +202,11 @@ void ColorSelector::onPaint(ui::PaintEvent& ev)

void ColorSelector::onPaintAlphaBar(ui::Graphics* g, const gfx::Rect& rc)
{
const int xmax = MAX(1, rc.w-1);
const int alpha = m_color.getAlpha();
const doc::color_t c =
(m_color.getType() != app::Color::MaskType ?
doc::rgba(m_color.getRed(),
m_color.getGreen(),
m_color.getBlue(), 255): 0);

for (int x=0; x<rc.w; ++x) {
const int a = (255 * x / xmax);

// TODO These values are hard-coded in rectgrid() (modules/gfx.cpp) too, use pref.xml values
const doc::color_t c1 = doc::rgba_blender_normal(gfx::rgba(128, 128, 128), c, a);
const doc::color_t c2 = doc::rgba_blender_normal(gfx::rgba(192, 192, 192), c, a);
const int mid = rc.h/2;
const int odd = (x / rc.h) & 1;
g->drawVLine(
app::color_utils::color_for_ui(app::Color::fromImage(IMAGE_RGB, odd ? c2: c1)),
rc.x+x, rc.y, mid);
g->drawVLine(
app::color_utils::color_for_ui(app::Color::fromImage(IMAGE_RGB, odd ? c1: c2)),
rc.x+x, rc.y+mid, rc.h-mid);
}
draw_alpha_slider(g, rc, m_color);

gfx::Point pos(rc.x + int(rc.w * alpha / 255),
rc.y + rc.h/2);
const int alpha = m_color.getAlpha();
const gfx::Point pos(rc.x + int(rc.w * alpha / 255),
rc.y + rc.h/2);
paintColorIndicator(g, pos, false);
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/ui/color_sliders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include "app/color_utils.h"
#include "app/modules/gfx.h"
#include "app/ui/color_sliders.h"
#include "app/ui/skin/skin_slider_property.h"
#include "app/ui/skin/skin_theme.h"
Expand Down Expand Up @@ -48,6 +49,12 @@ namespace {
}

void paint(Slider* slider, Graphics* g, const gfx::Rect& rc) {
// Special alpha bar (with two vertical lines)
if (m_channel == ColorSliders::Alpha) {
draw_alpha_slider(g, rc, m_color);
return;
}

gfx::Color color = gfx::ColorNone;
int w = MAX(rc.w-1, 1);

Expand Down Expand Up @@ -102,7 +109,6 @@ namespace {
break;

case ColorSliders::Gray:
case ColorSliders::Alpha:
color = color_utils::color_for_ui(
app::Color::fromGray(255 * x / w));
break;
Expand Down

0 comments on commit 629c359

Please sign in to comment.