diff --git a/src/app/color.cpp b/src/app/color.cpp index f06a1a200..5c5d05fb7 100644 --- a/src/app/color.cpp +++ b/src/app/color.cpp @@ -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() << " " diff --git a/src/app/color_utils.cpp b/src/app/color_utils.cpp index f839925e9..5d3bd3e51 100644 --- a/src/app/color_utils.cpp +++ b/src/app/color_utils.cpp @@ -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); diff --git a/src/app/color_utils.h b/src/app/color_utils.h index 7a790e6c2..767d8c97b 100644 --- a/src/app/color_utils.h +++ b/src/app/color_utils.h @@ -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); diff --git a/src/app/commands/cmd_mask_by_color.cpp b/src/app/commands/cmd_mask_by_color.cpp index 56d49f19f..25d86a698 100644 --- a/src/app/commands/cmd_mask_by_color.cpp +++ b/src/app/commands/cmd_mask_by_color.cpp @@ -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(new Mask()); diff --git a/src/app/script/brush_class.cpp b/src/app/script/brush_class.cpp index edfe2b718..28fdfaa56 100644 --- a/src/app/script/brush_class.cpp +++ b/src/app/script/brush_class.cpp @@ -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(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(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; } diff --git a/src/app/script/color_class.cpp b/src/app/script/color_class.cpp index ee0147a5d..ae5542955 100644 --- a/src/app/script/color_class.cpp +++ b/src/app/script/color_class.cpp @@ -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 diff --git a/src/app/script/engine.h b/src/app/script/engine.h index dfec00fb0..0860e773e 100644 --- a/src/app/script/engine.h +++ b/src/app/script/engine.h @@ -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 @@ -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); diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index a0fb28d44..0459be13b 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -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(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); diff --git a/src/app/script/palette_class.cpp b/src/app/script/palette_class.cpp index aef25b23d..05cf8ec8b 100644 --- a/src/app/script/palette_class.cpp +++ b/src/app/script/palette_class.cpp @@ -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 diff --git a/src/app/script/userdata.h b/src/app/script/userdata.h index 529bc8b47..4d7642c38 100644 --- a/src/app/script/userdata.h +++ b/src/app/script/userdata.h @@ -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 int UserData_set_color(lua_State* L) { auto obj = get_docobj(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(obj); UserData ud = wud->userData(); ud.setColor(docColor); diff --git a/src/app/ui/editor/editor_render.cpp b/src/app/ui/editor/editor_render.cpp index 35eaa28aa..45df1125b 100644 --- a/src/app/ui/editor/editor_render.cpp +++ b/src/app/ui/editor/editor_render.cpp @@ -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); }