Fix Palette:setColor() and Color Range command when alpha != 255

Use the app::Color alpha property to create the color for an
image (app::color_utils::color_for_image()) in several cases.

Fixed https://community.aseprite.org/t/4548
This commit is contained in:
David Capello 2020-01-08 17:40:43 -03:00
parent e68cc7dbf9
commit 70a211d978
11 changed files with 65 additions and 28 deletions

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -255,7 +256,7 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS
if (pixelFormat == IMAGE_INDEXED)
result << " Index "
<< color_utils::color_for_image(*this, pixelFormat);
<< color_utils::color_for_image(*this, IMAGE_INDEXED);
}
break;
@ -270,7 +271,7 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS
<< MID(0, int(m_value.hsv.v*100.0), 100) << "%";
if (pixelFormat == IMAGE_INDEXED)
result << " Index " << color_utils::color_for_image(*this, pixelFormat);
result << " Index " << color_utils::color_for_image(*this, IMAGE_INDEXED);
result << " (RGB "
<< getRed() << " "
@ -290,7 +291,7 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS
<< MID(0, int(m_value.hsl.l*100.0), 100) << "%";
if (pixelFormat == IMAGE_INDEXED)
result << " Index " << color_utils::color_for_image(*this, pixelFormat);
result << " Index " << color_utils::color_for_image(*this, IMAGE_INDEXED);
result << " (RGB "
<< getRed() << " "

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
@ -89,6 +90,28 @@ doc::color_t color_utils::color_for_image(const app::Color& color, PixelFormat f
doc::color_t c = -1;
switch (format) {
case IMAGE_RGB:
c = doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
break;
case IMAGE_GRAYSCALE:
c = doc::graya(color.getGray(), color.getAlpha());
break;
case IMAGE_INDEXED:
c = color.getIndex();
break;
}
return c;
}
doc::color_t color_utils::color_for_image_without_alpha(const app::Color& color, PixelFormat format)
{
if (color.getType() == app::Color::MaskType)
return 0;
doc::color_t c = -1;
switch (format) {
case IMAGE_RGB:
c = doc::rgba(color.getRed(), color.getGreen(), color.getBlue(), 255);

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2020 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello
//
// This program is distributed under the terms of
@ -26,6 +27,7 @@ namespace app {
gfx::Color color_for_ui(const app::Color& color);
doc::color_t color_for_image(const app::Color& color, doc::PixelFormat format);
doc::color_t color_for_image_without_alpha(const app::Color& color, doc::PixelFormat format);
doc::color_t color_for_layer(const app::Color& color, doc::Layer* layer);
doc::color_t color_for_target_mask(const app::Color& color, const ColorTarget& colorTarget);
doc::color_t color_for_target(const app::Color& color, const ColorTarget& colorTarget);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -207,7 +207,8 @@ Mask* MaskByColorCommand::generateMask(const Mask& origMask,
int xpos, int ypos,
gen::SelectionMode mode)
{
int color = color_utils::color_for_image(m_buttonColor->getColor(), sprite->pixelFormat());
int color = color_utils::color_for_image(m_buttonColor->getColor(),
sprite->pixelFormat());
int tolerance = m_sliderTolerance->getValue();
std::unique_ptr<Mask> mask(new Mask());

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
@ -127,8 +127,10 @@ int Brush_gc(lua_State* L)
int Brush_setFgColor(lua_State* L)
{
auto obj = get_obj<BrushObj>(L, 1);
if (obj->brush) {
const doc::color_t color = convert_args_into_pixel_color(L, 2);
if (obj->brush &&
obj->brush->image()) {
const doc::color_t color = convert_args_into_pixel_color(
L, 2, obj->brush->image()->pixelFormat());
obj->brush->setImageColor(Brush::ImageColor::MainColor, color);
}
return 0;
@ -137,9 +139,12 @@ int Brush_setFgColor(lua_State* L)
int Brush_setBgColor(lua_State* L)
{
auto obj = get_obj<BrushObj>(L, 1);
if (obj->brush) {
const doc::color_t color = convert_args_into_pixel_color(L, 2);
obj->brush->setImageColor(Brush::ImageColor::BackgroundColor, color);
if (obj->brush &&
obj->brush->image()) {
const doc::color_t color = convert_args_into_pixel_color(
L, 2, obj->brush->image()->pixelFormat());
obj->brush->setImageColor(
Brush::ImageColor::BackgroundColor, color);
}
return 0;
}

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -481,10 +481,11 @@ app::Color convert_args_into_color(lua_State* L, int index)
return Color_new(L, index);
}
doc::color_t convert_args_into_pixel_color(lua_State* L, int index)
doc::color_t convert_args_into_pixel_color(lua_State* L, int index,
const doc::PixelFormat pixelFormat)
{
app::Color color = convert_args_into_color(L, index);
return color_utils::color_for_image(color, doc::IMAGE_RGB);
return color_utils::color_for_image(color, pixelFormat);
}
} // namespace script

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -18,6 +18,7 @@
#include "doc/brush.h"
#include "doc/frame.h"
#include "doc/object_ids.h"
#include "doc/pixel_format.h"
#include "gfx/fwd.h"
#include <cstdio>
@ -149,7 +150,8 @@ namespace app {
gfx::Rect convert_args_into_rect(lua_State* L, int index);
gfx::Size convert_args_into_size(lua_State* L, int index);
app::Color convert_args_into_color(lua_State* L, int index);
doc::color_t convert_args_into_pixel_color(lua_State* L, int index);
doc::color_t convert_args_into_pixel_color(lua_State* L, int index,
const doc::PixelFormat pixelFormat);
doc::Palette* get_palette_from_arg(lua_State* L, int index);
doc::Image* may_get_image_from_arg(lua_State* L, int index);
doc::Image* get_image_from_arg(lua_State* L, int index);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2015-2018 David Capello
//
// This program is distributed under the terms of
@ -164,7 +164,7 @@ int Image_clear(lua_State* L)
else if (lua_isinteger(L, 2))
color = lua_tointeger(L, 2);
else
color = convert_args_into_pixel_color(L, 2);
color = convert_args_into_pixel_color(L, 2, img->pixelFormat());
doc::clear_image(img, color);
return 0;
}
@ -172,14 +172,15 @@ int Image_clear(lua_State* L)
int Image_drawPixel(lua_State* L)
{
auto obj = get_obj<ImageObj>(L, 1);
auto img = obj->image(L);
const int x = lua_tointeger(L, 2);
const int y = lua_tointeger(L, 3);
doc::color_t color;
if (lua_isinteger(L, 4))
color = lua_tointeger(L, 4);
else
color = convert_args_into_pixel_color(L, 4);
doc::put_pixel(obj->image(L), x, y, color);
color = convert_args_into_pixel_color(L, 4, img->pixelFormat());
doc::put_pixel(img, x, y, color);
return 0;
}
@ -304,7 +305,7 @@ int Image_isPlain(lua_State* L)
else if (lua_isinteger(L, 2))
color = lua_tointeger(L, 2);
else
color = convert_args_into_pixel_color(L, 2);
color = convert_args_into_pixel_color(L, 2, img->pixelFormat());
bool res = doc::is_plain_image(img, color);
lua_pushboolean(L, res);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -201,7 +201,8 @@ int Palette_setColor(lua_State* L)
if (i < 0 || i >= int(pal->size()))
return luaL_error(L, "index out of bounds %d", i);
doc::color_t docColor = convert_args_into_pixel_color(L, 3);
doc::color_t docColor = convert_args_into_pixel_color(
L, 3, doc::IMAGE_RGB);
if (auto sprite = obj->sprite(L)) {
// Nothing to do

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -67,7 +67,7 @@ int UserData_set_text(lua_State* L) {
template<typename T>
int UserData_set_color(lua_State* L) {
auto obj = get_docobj<T>(L, 1);
doc::color_t docColor = convert_args_into_pixel_color(L, 2);
doc::color_t docColor = convert_args_into_pixel_color(L, 2, doc::IMAGE_RGB);
auto wud = get_WithUserData<T>(obj);
UserData ud = wud->userData();
ud.setColor(docColor);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -88,8 +88,8 @@ void EditorRender::setupBackground(Doc* doc, doc::PixelFormat pixelFormat)
m_render->setBgType(bgType);
m_render->setBgZoom(docPref.bg.zoom());
m_render->setBgColor1(color_utils::color_for_image(docPref.bg.color1(), pixelFormat));
m_render->setBgColor2(color_utils::color_for_image(docPref.bg.color2(), pixelFormat));
m_render->setBgColor1(color_utils::color_for_image_without_alpha(docPref.bg.color1(), pixelFormat));
m_render->setBgColor2(color_utils::color_for_image_without_alpha(docPref.bg.color2(), pixelFormat));
m_render->setBgCheckedSize(tile);
}