lua: Add ColorSpace class

This commit is contained in:
David Capello 2019-01-03 17:17:10 -03:00
parent aff3018095
commit 0fef9bfa6b
6 changed files with 183 additions and 5 deletions

View File

@ -1,5 +1,5 @@
# Aseprite
# Copyright (C) 2018 Igara Studio S.A.
# Copyright (C) 2018-2019 Igara Studio S.A.
# Copyright (C) 2001-2018 David Capello
# Generate a ui::Widget for each widget in a XML file
@ -153,6 +153,7 @@ if(ENABLE_SCRIPTING)
script/cel_class.cpp
script/cels_class.cpp
script/color_class.cpp
script/color_space_class.cpp
script/dialog_class.cpp
script/engine.cpp
script/frame_class.cpp

View File

@ -0,0 +1,112 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app.h"
#include "app/script/luacpp.h"
#include "base/file_content.h"
namespace app {
namespace script {
namespace {
int ColorSpace_new(lua_State* L)
{
// Copy color space
if (auto cs2 = may_get_obj<gfx::ColorSpace>(L, 1)) {
push_new<gfx::ColorSpace>(L, *cs2);
return 1;
}
else if (lua_istable(L, 1)) {
// Load ICC profile when ColorSpace{ filename="..." } is specified
if (lua_getfield(L, 1, "filename") != LUA_TNIL) {
const char* fn = lua_tostring(L, -1);
if (fn) {
auto buf = base::read_file_content(fn);
lua_pop(L, 1);
push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeICC(std::move(buf)));
return 1;
}
}
else
lua_pop(L, 1);
// Create sRGB profile with ColorSpace{ sRGB }
if (lua_getfield(L, 1, "sRGB") != LUA_TNONE) {
lua_pop(L, 1);
push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeSRGB());
return 1;
}
else
lua_pop(L, 1);
}
push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeNone());
return 1;
}
int ColorSpace_gc(lua_State* L)
{
get_obj<gfx::ColorSpace>(L, 1)->~ColorSpace();
return 0;
}
int ColorSpace_eq(lua_State* L)
{
const auto a = get_obj<gfx::ColorSpace>(L, 1);
const auto b = get_obj<gfx::ColorSpace>(L, 2);
lua_pushboolean(L, a->nearlyEqual(*b));
return 1;
}
int ColorSpace_get_name(lua_State* L)
{
const auto cs = get_obj<gfx::ColorSpace>(L, 1);
lua_pushstring(L, cs->name().c_str());
return 1;
}
int ColorSpace_set_name(lua_State* L)
{
auto cs = get_obj<gfx::ColorSpace>(L, 1);
if (auto name = lua_tostring(L, 2))
cs->setName(name);
return 0;
}
const luaL_Reg ColorSpace_methods[] = {
{ "__gc", ColorSpace_gc },
{ "__eq", ColorSpace_eq },
{ nullptr, nullptr }
};
const Property ColorSpace_properties[] = {
{ "name", ColorSpace_get_name, ColorSpace_set_name },
{ nullptr, nullptr, nullptr }
};
} // anonymous namespace
DEF_MTNAME(gfx::ColorSpace);
void register_color_space_class(lua_State* L)
{
using ColorSpace = gfx::ColorSpace;
REG_CLASS(L, ColorSpace);
REG_CLASS_NEW(L, ColorSpace);
REG_CLASS_PROPERTIES(L, ColorSpace);
}
void push_color_space(lua_State* L, const gfx::ColorSpace& cs)
{
push_new<gfx::ColorSpace>(L, cs);
}
} // namespace script
} // namespace app

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -137,6 +137,7 @@ void register_app_command_object(lua_State* L);
void register_cel_class(lua_State* L);
void register_cels_class(lua_State* L);
void register_color_class(lua_State* L);
void register_color_space_class(lua_State* L);
void register_dialog_class(lua_State* L);
void register_frame_class(lua_State* L);
void register_frames_class(lua_State* L);
@ -281,6 +282,7 @@ Engine::Engine()
register_cel_class(L);
register_cels_class(L);
register_color_class(L);
register_color_space_class(L);
register_dialog_class(L);
register_frame_class(L);
register_frames_class(L);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -25,6 +25,10 @@
struct lua_State;
namespace gfx {
class ColorSpace;
}
namespace doc {
class Cel;
class FrameTag;
@ -103,6 +107,7 @@ namespace app {
void push_cels(lua_State* L, const doc::ObjectIds& cels);
void push_cels(lua_State* L, doc::Layer* layer);
void push_cels(lua_State* L, doc::Sprite* sprite);
void push_color_space(lua_State* L, const gfx::ColorSpace& cs);
void push_doc_range(lua_State* L, Site& site, const DocRange& docRange);
void push_images(lua_State* L, const doc::ObjectIds& images);
void push_layers(lua_State* L, const doc::ObjectIds& layers);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (c) 2018 Igara Studio S.A.
// Copyright (c) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -9,6 +9,7 @@
#include "config.h"
#endif
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "doc/image_spec.h"
@ -72,6 +73,16 @@ int ImageSpec_get_colorMode(lua_State* L)
return 1;
}
int ImageSpec_get_colorSpace(lua_State* L)
{
const auto spec = get_obj<doc::ImageSpec>(L, 1);
if (spec->colorSpace())
push_color_space(L, *spec->colorSpace());
else
lua_pushnil(L);
return 1;
}
int ImageSpec_get_width(lua_State* L)
{
const auto spec = get_obj<doc::ImageSpec>(L, 1);
@ -100,6 +111,14 @@ int ImageSpec_set_colorMode(lua_State* L)
return 0;
}
int ImageSpec_set_colorSpace(lua_State* L)
{
auto spec = get_obj<doc::ImageSpec>(L, 1);
auto cs = get_obj<gfx::ColorSpace>(L, 2);
spec->setColorSpace(std::make_shared<gfx::ColorSpace>(*cs));
return 0;
}
int ImageSpec_set_width(lua_State* L)
{
auto spec = get_obj<doc::ImageSpec>(L, 1);
@ -129,6 +148,7 @@ const luaL_Reg ImageSpec_methods[] = {
const Property ImageSpec_properties[] = {
{ "colorMode", ImageSpec_get_colorMode, ImageSpec_set_colorMode },
{ "colorSpace", ImageSpec_get_colorSpace, ImageSpec_set_colorSpace },
{ "width", ImageSpec_get_width, ImageSpec_set_width },
{ "height", ImageSpec_get_height, ImageSpec_set_height },
{ "transparentColor", ImageSpec_get_transparentColor, ImageSpec_set_transparentColor },

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2015-2018 David Capello
//
// This program is distributed under the terms of
@ -11,7 +11,9 @@
#include "app/app.h"
#include "app/cmd/add_layer.h"
#include "app/cmd/assign_color_profile.h"
#include "app/cmd/clear_cel.h"
#include "app/cmd/convert_color_profile.h"
#include "app/cmd/remove_frame_tag.h"
#include "app/cmd/remove_layer.h"
#include "app/cmd/remove_slice.h"
@ -225,6 +227,28 @@ int Sprite_setPalette(lua_State* L)
return 0;
}
int Sprite_assignColorSpace(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
auto cs = get_obj<gfx::ColorSpace>(L, 2);
Tx tx;
tx(new cmd::AssignColorProfile(
sprite, std::make_shared<gfx::ColorSpace>(*cs)));
tx.commit();
return 1;
}
int Sprite_convertColorSpace(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
auto cs = get_obj<gfx::ColorSpace>(L, 2);
Tx tx;
tx(new cmd::ConvertColorProfile(
sprite, std::make_shared<gfx::ColorSpace>(*cs)));
tx.commit();
return 1;
}
int Sprite_newLayer(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
@ -492,6 +516,17 @@ int Sprite_get_colorMode(lua_State* L)
return 1;
}
int Sprite_get_colorSpace(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
auto cs = sprite->colorSpace();
if (cs)
push_color_space(L, *cs);
else
lua_pushnil(L);
return 1;
}
int Sprite_get_spec(lua_State* L)
{
const auto sprite = get_docobj<Sprite>(L, 1);
@ -612,6 +647,8 @@ const luaL_Reg Sprite_methods[] = {
{ "close", Sprite_close },
{ "loadPalette", Sprite_loadPalette },
{ "setPalette", Sprite_setPalette },
{ "assignColorSpace", Sprite_assignColorSpace },
{ "convertColorSpace", Sprite_convertColorSpace },
// Layers
{ "newLayer", Sprite_newLayer },
{ "newGroup", Sprite_newGroup },
@ -637,6 +674,7 @@ const Property Sprite_properties[] = {
{ "width", Sprite_get_width, Sprite_set_width },
{ "height", Sprite_get_height, Sprite_set_height },
{ "colorMode", Sprite_get_colorMode, nullptr },
{ "colorSpace", Sprite_get_colorSpace, Sprite_assignColorSpace },
{ "spec", Sprite_get_spec, nullptr },
{ "selection", Sprite_get_selection, nullptr },
{ "frames", Sprite_get_frames, nullptr },