Replace AssColor with agi::Color

Add agi::Color, and replace AssColor and all uses of wxColor that are
not immediately passed to/from wx with it.
This commit is contained in:
Thomas Goyne 2012-10-26 07:09:14 -07:00
parent 83761d881a
commit ea5428b65f
41 changed files with 628 additions and 626 deletions

View file

@ -283,6 +283,10 @@
RelativePath="..\..\libaegisub\common\charset_ucd.cpp"
>
</File>
<File
RelativePath="..\..\libaegisub\common\color.cpp"
>
</File>
<File
RelativePath="..\..\libaegisub\common\hotkey.cpp"
>
@ -418,7 +422,7 @@
>
</File>
<File
RelativePath="..\..\libaegisub\include\libaegisub\colour.h"
RelativePath="..\..\libaegisub\include\libaegisub\color.h"
>
</File>
<File

View file

@ -23,6 +23,7 @@ SRC += \
common/charset_6937.cpp \
common/charset_conv.cpp \
common/charset_ucd.cpp \
common/color.cpp \
common/hotkey.cpp \
common/io.cpp \
common/json.cpp \

View file

@ -0,0 +1,146 @@
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "../config.h"
#include "libaegisub/color.h"
#include <boost/config/warning_disable.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
BOOST_FUSION_ADAPT_STRUCT(
agi::Color,
(unsigned char, r)
(unsigned char, g)
(unsigned char, b)
(unsigned char, a)
)
namespace {
using namespace boost::spirit;
struct unpack_colors : public boost::static_visitor<agi::Color> {
template<typename T> struct result { typedef agi::Color type; };
template<class T> agi::Color operator()(T arg) const {
return boost::apply_visitor(*this, arg);
}
agi::Color operator()(int abgr) const { return (*this)((unsigned)abgr); }
agi::Color operator()(unsigned int abgr) const {
return agi::Color(abgr & 0xFF, (abgr >> 8) & 0xFF, (abgr >> 16) & 0xFF, (abgr >> 24) & 0xFF);
}
};
template<typename Iterator>
struct color_grammar : qi::grammar<Iterator, agi::Color()> {
qi::rule<Iterator, agi::Color()> color;
qi::rule<Iterator, agi::Color()> css_color;
qi::rule<Iterator, agi::Color()> ass_color;
qi::rule<Iterator, unsigned char()> rgb_component;
qi::rule<Iterator, unsigned char()> rgb_percent;
qi::rule<Iterator, unsigned char()> hex_byte;
qi::rule<Iterator, unsigned char()> hex_char;
qi::rule<Iterator> comma;
qi::rule<Iterator> blank;
#define HEX_PARSER(type, len) qi::uint_parser<unsigned type, 16, len, len>()
color_grammar() : color_grammar::base_type(color) {
color = css_color | ass_color;
boost::phoenix::function<unpack_colors> unpack;
ass_color = (
int_
| -lit('&') >> -(lit('H') | lit('h')) >> (HEX_PARSER(int, 8) | HEX_PARSER(int, 6)) >> -lit('&')
)[_val = unpack(_1)] >> blank >> qi::eoi;
css_color
= "rgb(" >> blank >> rgb_component >> comma >> rgb_component >> comma >> rgb_component >> blank >> ')'
| '#' >> hex_byte >> hex_byte >> hex_byte
| '#' >> hex_char >> hex_char >> hex_char
;
hex_char = HEX_PARSER(int, 1)[_val = _1 * 16 + _1];
hex_byte = HEX_PARSER(int, 2);
rgb_component = qi::uint_parser<unsigned char, 10, 1, 3>();
comma = *qi::blank >> "," >> *qi::blank;
blank = *qi::blank;
}
};
}
namespace agi {
Color::Color() : r(0), g(0), b(0), a(0) { }
Color::Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
: r(r), g(g), b(b), a(a)
{ }
Color::Color(std::string const& str)
: r(0), g(0), b(0), a(0)
{
const char *begin = &str[0];
parse(begin, &str[str.size()], color_grammar<const char *>(), *this);
}
Color::Color(const char *str)
: r(0), g(0), b(0), a(0)
{
parse(str, str + strlen(str), color_grammar<const char *>(), *this);
}
std::string Color::GetAssStyleFormatted() const {
return str(boost::format("&H%02X%02X%02X%02X") % (int)a % (int)b % (int)g % (int)r);
}
std::string Color::GetAssOverrideFormatted() const {
return str(boost::format("&H%02X%02X%02X&") % (int)b % (int)g % (int)r);
}
std::string Color::GetSsaFormatted() const {
return boost::lexical_cast<std::string>((a << 24) + (b << 16) + (g << 8) + r);
}
std::string Color::GetHexFormatted() const {
return str(boost::format("#%02X%02X%02X") % (int)r % (int)g % (int)b);
}
std::string Color::GetRgbFormatted() const {
return str(boost::format("rgb(%d, %d, %d)") % (int)r % (int)g % (int)b);
}
bool Color::operator==(Color const& col) const {
return r == col.r && g == col.g && b == col.b && a == col.a;
}
bool Color::operator!=(Color const& col) const {
return !(*this == col);
}
}

View file

@ -63,7 +63,7 @@ namespace {
json::Array array;
for (typename std::vector<T>::const_iterator it = value.begin(); it != value.end(); ++it) {
array.push_back(json::Object());
static_cast<json::Object&>(array.back())[element_key] = *it;
static_cast<json::Object&>(array.back())[element_key] = (json::UnknownElement)*it;
}
put_option(obj, path, array);
@ -151,8 +151,8 @@ void Options::Flush() {
put_option(obj_out, i->first, i->second->GetDouble());
break;
case OptionValue::Type_Colour:
put_option(obj_out, i->first, i->second->GetColour());
case OptionValue::Type_Color:
put_option(obj_out, i->first, i->second->GetColor().GetRgbFormatted());
break;
case OptionValue::Type_Bool:
@ -171,8 +171,8 @@ void Options::Flush() {
put_array(obj_out, i->first, "double", i->second->GetListDouble());
break;
case OptionValue::Type_List_Colour:
put_array(obj_out, i->first, "colour", i->second->GetListColour());
case OptionValue::Type_List_Color:
put_array(obj_out, i->first, "color", i->second->GetListColor());
break;
case OptionValue::Type_List_Bool:

View file

@ -26,7 +26,7 @@
#include <cmath>
#endif
#include <libaegisub/colour.h>
#include <libaegisub/color.h>
#include <libaegisub/log.h>
#include <libaegisub/option_value.h>
#include <libaegisub/scoped_ptr.h>
@ -78,7 +78,7 @@ OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const&
return 0;
}
arr.push_back(obj.begin()->second);
arr.push_back(ValueType(obj.begin()->second));
}
return new OptionValueType(name, arr);
@ -106,8 +106,8 @@ void ConfigVisitor::Visit(const json::Array& array) {
AddOptionValue(ReadArray(array, array_type, &OptionValueListDouble::SetListDouble));
else if (array_type == "bool")
AddOptionValue(ReadArray(array, array_type, &OptionValueListBool::SetListBool));
else if (array_type == "colour")
AddOptionValue(ReadArray(array, array_type, &OptionValueListColour::SetListColour));
else if (array_type == "color")
AddOptionValue(ReadArray(array, array_type, &OptionValueListColor::SetListColor));
else
Error<OptionJsonValueArray>("Array type not handled");
}
@ -121,8 +121,8 @@ void ConfigVisitor::Visit(const json::Double& number) {
}
void ConfigVisitor::Visit(const json::String& string) {
if (string.find("rgb(") == 0) {
AddOptionValue(new OptionValueColour(name, string));
if (string.size() && (string.find("rgb(") == 0 || string[0] == '#' || string[0] == '&')) {
AddOptionValue(new OptionValueColor(name, string));
} else {
AddOptionValue(new OptionValueString(name, string));
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@ -12,18 +12,33 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// @file colour.h
/// @brief Colourspace class and functions.
/// @ingroup libaegisub
#pragma once
#ifndef LAGI_PRE
#include <string>
#endif
// This file is a stub for now.
namespace agi {
struct Color {
unsigned char r; ///< Red component
unsigned char g; ///< Green component
unsigned char b; ///< Blue component
unsigned char a; ///< Alpha component
typedef std::string Colour;
Color();
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0);
Color(std::string const& str);
Color(const char *str);
} // namespace agi
bool operator==(Color const& col) const;
bool operator!=(Color const& col) const;
std::string GetAssStyleFormatted() const;
std::string GetAssOverrideFormatted() const;
std::string GetSsaFormatted() const;
std::string GetHexFormatted() const;
std::string GetRgbFormatted() const;
operator std::string() const { return GetRgbFormatted(); }
};
}

View file

@ -23,7 +23,7 @@
#include <vector>
#endif
#include <libaegisub/colour.h>
#include <libaegisub/color.h>
#include <libaegisub/exception.h>
#include <libaegisub/signal.h>
@ -59,12 +59,12 @@ public:
Type_String = 0, ///< String
Type_Int = 1, ///< Integer
Type_Double = 2, ///< Double
Type_Colour = 3, ///< Colour
Type_Color = 3, ///< Color
Type_Bool = 4, ///< Bool
Type_List_String = 100, ///< List of Strings
Type_List_Int = 101, ///< List of Integers
Type_List_Double = 102, ///< List of Doubles
Type_List_Colour = 103, ///< List of Colours
Type_List_Color = 103, ///< List of Colors
Type_List_Bool = 104 ///< List of Bools
};
@ -76,38 +76,38 @@ public:
virtual std::string GetString() const { throw TypeError("string"); }
virtual int64_t GetInt() const { throw TypeError("int"); }
virtual double GetDouble() const { throw TypeError("double"); }
virtual Colour GetColour() const { throw TypeError("colour"); }
virtual Color GetColor() const { throw TypeError("color"); }
virtual bool GetBool() const { throw TypeError("bool"); }
virtual void SetString(const std::string) { throw TypeError("string", " set "); }
virtual void SetInt(const int64_t) { throw TypeError("int", " set "); }
virtual void SetDouble(const double) { throw TypeError("double", " set "); }
virtual void SetColour(const Colour) { throw TypeError("colour", " set "); }
virtual void SetColor(const Color) { throw TypeError("color", " set "); }
virtual void SetBool(const bool) { throw TypeError("bool", " set "); }
virtual std::string GetDefaultString() const { throw TypeError("string"); }
virtual int64_t GetDefaultInt() const { throw TypeError("int"); }
virtual double GetDefaultDouble() const { throw TypeError("double"); }
virtual Colour GetDefaultColour() const { throw TypeError("colour"); }
virtual Color GetDefaultColor() const { throw TypeError("color"); }
virtual bool GetDefaultBool() const { throw TypeError("bool"); }
virtual std::vector<std::string> const& GetListString() const { throw ListTypeError("string"); }
virtual std::vector<int64_t> const& GetListInt() const { throw ListTypeError("int"); }
virtual std::vector<double> const& GetListDouble() const { throw ListTypeError("double"); }
virtual std::vector<Colour> const& GetListColour() const { throw ListTypeError("colour"); }
virtual std::vector<Color> const& GetListColor() const { throw ListTypeError("color"); }
virtual std::vector<bool> const& GetListBool() const { throw ListTypeError("string"); }
virtual void SetListString(const std::vector<std::string>&) { throw ListTypeError("string", " set "); }
virtual void SetListInt(const std::vector<int64_t>&) { throw ListTypeError("int", " set "); }
virtual void SetListDouble(const std::vector<double>&) { throw ListTypeError("double", " set "); }
virtual void SetListColour(const std::vector<Colour>&) { throw ListTypeError("colour", " set "); }
virtual void SetListColor(const std::vector<Color>&) { throw ListTypeError("color", " set "); }
virtual void SetListBool(const std::vector<bool>&) { throw ListTypeError("string", " set "); }
virtual std::vector<std::string> const& GetDefaultListString() const { throw ListTypeError("string"); }
virtual std::vector<int64_t> const& GetDefaultListInt() const { throw ListTypeError("int"); }
virtual std::vector<double> const& GetDefaultListDouble() const { throw ListTypeError("double"); }
virtual std::vector<Colour> const& GetDefaultListColour() const { throw ListTypeError("colour"); }
virtual std::vector<Color> const& GetDefaultListColor() const { throw ListTypeError("color"); }
virtual std::vector<bool> const& GetDefaultListBool() const { throw ListTypeError("string"); }
virtual void Set(const OptionValue *new_value)=0;
@ -136,7 +136,7 @@ public:
CONFIG_OPTIONVALUE(String, std::string)
CONFIG_OPTIONVALUE(Int, int64_t)
CONFIG_OPTIONVALUE(Double, double)
CONFIG_OPTIONVALUE(Colour, Colour)
CONFIG_OPTIONVALUE(Color, Color)
CONFIG_OPTIONVALUE(Bool, bool)
#define CONFIG_OPTIONVALUE_LIST(type_name, type) \
@ -161,7 +161,7 @@ CONFIG_OPTIONVALUE(Bool, bool)
CONFIG_OPTIONVALUE_LIST(String, std::string)
CONFIG_OPTIONVALUE_LIST(Int, int64_t)
CONFIG_OPTIONVALUE_LIST(Double, double)
CONFIG_OPTIONVALUE_LIST(Colour, Colour)
CONFIG_OPTIONVALUE_LIST(Color, Color)
CONFIG_OPTIONVALUE_LIST(Bool, bool)
} // namespace agi

View file

@ -42,91 +42,10 @@
#endif
#include "ass_style.h"
#include "compat.h"
#include "subtitle_format.h"
#include "utils.h"
AssColor::AssColor () {
r=g=b=a=0;
}
AssColor::AssColor(int r, int g, int b, int a)
: r(r)
, g(g)
, b(b)
, a(a)
{
}
AssColor::AssColor(const wxColour &color)
: a(0)
{
SetWXColor(color);
}
void AssColor::Parse(wxString const& value) {
if (value.size() > 0 && value[0] == '#') {
// HTML colour
SetWXColor(wxColor(value));
return;
}
// Prepare
char ostr[12];
int oindex = 11;
bool ishex = false;
ostr[11] = 0;
for(size_t i = value.size(); i > 0 && oindex >= 0; i--) {
unsigned char c = value[i - 1];
if (isxdigit(c) || c == '-') {
ostr[--oindex] = c;
if (c >= 'A')
ishex = true;
}
else if (c == 'H' || c == 'h')
ishex = true;
}
unsigned long outval = strtoul(ostr + oindex, 0, ishex ? 16 : 10);
r = outval & 0xFF;
g = (outval>>8) & 0xFF;
b = (outval>>16) & 0xFF;
a = (outval>>24) & 0xFF;
}
wxColour AssColor::GetWXColor() const {
return wxColour(r,g,b,255-a);
}
void AssColor::SetWXColor(const wxColor &color) {
r = color.Red();
g = color.Green();
b = color.Blue();
}
wxString AssColor::GetAssFormatted(bool alpha,bool stripped,bool isStyle) const {
wxString work;
if (!stripped) work += "&H";
if (alpha) work += wxString::Format("%02X",a);
work += wxString::Format("%02X%02X%02X",b,g,r);
if (!stripped && !isStyle) work += "&";
return work;
}
wxString AssColor::GetSSAFormatted() const {
long color = (a<<24)+(b<<16)+(g<<8)+r;
wxString output=wxString::Format("%li",(long)color);
return output;
}
bool AssColor::operator==(const AssColor &col) const {
return r==col.r && g==col.g && b==col.b && a==col.a;
}
bool AssColor::operator!=(const AssColor &col) const {
return !(*this == col);
}
AssStyle::AssStyle()
: AssEntry(wxString(), wxS("[V4+ Styles]"))
, name("Default")
@ -134,8 +53,6 @@ AssStyle::AssStyle()
, fontsize(20.)
, primary(255, 255, 255)
, secondary(255, 0, 0)
, outline(0, 0, 0)
, shadow(0, 0, 0)
, bold(false)
, italic(false)
, underline(false)
@ -184,20 +101,20 @@ AssStyle::AssStyle(wxString rawData, int version)
fontsize = get_next_double(tkn);
if (version != 0) {
primary.Parse(get_next_string(tkn));
secondary.Parse(get_next_string(tkn));
outline.Parse(get_next_string(tkn));
shadow.Parse(get_next_string(tkn));
primary = from_wx(get_next_string(tkn));
secondary = from_wx(get_next_string(tkn));
outline = from_wx(get_next_string(tkn));
shadow = from_wx(get_next_string(tkn));
}
else {
primary.Parse(get_next_string(tkn));
secondary.Parse(get_next_string(tkn));
primary = from_wx(get_next_string(tkn));
secondary = from_wx(get_next_string(tkn));
// Read and discard tertiary color
get_next_string(tkn);
// Read shadow/outline color
outline.Parse(get_next_string(tkn));
outline = from_wx(get_next_string(tkn));
shadow = outline;
}
@ -257,16 +174,16 @@ AssStyle::AssStyle(wxString rawData, int version)
void AssStyle::UpdateData() {
wxString final;
name.Replace(",",";");
font.Replace(",",";");
//name.Replace(",",";");
//font.Replace(",",";");
final = wxString::Format("Style: %s,%s,%g,%s,%s,%s,%s,%d,%d,%d,%d,%g,%g,%g,%g,%d,%g,%g,%i,%i,%i,%i,%i",
name, font, fontsize,
primary.GetAssFormatted(true,false,true),
secondary.GetAssFormatted(true,false,true),
outline.GetAssFormatted(true,false,true),
shadow.GetAssFormatted(true,false,true),
primary.GetAssStyleFormatted(),
secondary.GetAssStyleFormatted(),
outline.GetAssStyleFormatted(),
shadow.GetAssStyleFormatted(),
(bold? -1 : 0), (italic ? -1 : 0),
(underline?-1:0),(strikeout?-1:0),
scalex,scaley,spacing,angle,
@ -286,9 +203,9 @@ wxString AssStyle::GetSSAText() const {
output = wxString::Format("Style: %s,%s,%g,%s,%s,0,%s,%d,%d,%d,%g,%g,%d,%d,%d,%d,0,%i",
n, f, fontsize,
primary.GetSSAFormatted(),
secondary.GetSSAFormatted(),
shadow.GetSSAFormatted(),
primary.GetSsaFormatted(),
secondary.GetSsaFormatted(),
shadow.GetSsaFormatted(),
(bold? -1 : 0), (italic ? -1 : 0),
borderstyle,outline_w,shadow_w,align,
Margin[0],Margin[1],Margin[2],encoding);

View file

@ -40,25 +40,7 @@
#include "ass_entry.h"
struct AssColor {
int r; ///< Red component
int g; ///< Green component
int b; ///< Blue component
int a; ///< Alpha component
AssColor();
AssColor(int r, int g, int b, int a = 0);
AssColor(const wxColour &color);
bool operator==(const AssColor &col) const;
bool operator!=(const AssColor &col) const;
wxColor GetWXColor() const; // Return as a wxColor
void SetWXColor(const wxColor &color); // Sets from a wxColor
void Parse(wxString const& value); // Parse SSA or ASS-style color
wxString GetAssFormatted(bool alpha,bool stripped=false,bool isStyle=false) const; // Gets color formated in ASS format
wxString GetSSAFormatted() const;
};
#include <libaegisub/color.h>
class AssStyle : public AssEntry {
public:
@ -66,10 +48,10 @@ public:
wxString font; ///< Font face name
double fontsize; ///< Font size
AssColor primary; ///< Default text color
AssColor secondary; ///< Text color for not-yet-reached karaoke syllables
AssColor outline; ///< Outline color
AssColor shadow; ///< Shadow color
agi::Color primary; ///< Default text color
agi::Color secondary; ///< Text color for not-yet-reached karaoke syllables
agi::Color outline; ///< Outline color
agi::Color shadow; ///< Shadow color
bool bold;
bool italic;

View file

@ -86,14 +86,14 @@ public:
void SetColourScheme(std::string const& name)
{
std::string opt_prefix = "Colour/Schemes/" + name + "/UI/";
light_colour = lagi_wxColour(OPT_GET(opt_prefix + "Light")->GetColour());
dark_colour = lagi_wxColour(OPT_GET(opt_prefix + "Dark")->GetColour());
sel_colour = lagi_wxColour(OPT_GET(opt_prefix + "Selection")->GetColour());
light_colour = to_wx(OPT_GET(opt_prefix + "Light")->GetColor());
dark_colour = to_wx(OPT_GET(opt_prefix + "Dark")->GetColor());
sel_colour = to_wx(OPT_GET(opt_prefix + "Selection")->GetColor());
opt_prefix = "Colour/Schemes/" + name + "/UI Focused/";
light_focused_colour = lagi_wxColour(OPT_GET(opt_prefix + "Light")->GetColour());
dark_focused_colour = lagi_wxColour(OPT_GET(opt_prefix + "Dark")->GetColour());
sel_focused_colour = lagi_wxColour(OPT_GET(opt_prefix + "Selection")->GetColour());
light_focused_colour = to_wx(OPT_GET(opt_prefix + "Light")->GetColor());
dark_focused_colour = to_wx(OPT_GET(opt_prefix + "Dark")->GetColor());
sel_focused_colour = to_wx(OPT_GET(opt_prefix + "Selection")->GetColor());
}
/// Set whether to use the focused or unfocused colours

View file

@ -750,8 +750,8 @@ namespace Automation4 {
// LuaFeature
LuaFeature::LuaFeature(lua_State *L)
: L(L)
, myid(0)
: myid(0)
, L(L)
{
}

View file

@ -112,7 +112,7 @@ namespace {
return BadField(std::string("Invalid or missing field '") + name + "' in '" + line_clasee + "' class subtitle line (expected " + expected_type + ")");
}
wxString get_string_field(lua_State *L, const char *name, const char *line_class)
wxString get_wxstring_field(lua_State *L, const char *name, const char *line_class)
{
lua_getfield(L, -1, name);
if (!lua_isstring(L, -1))
@ -122,6 +122,16 @@ namespace {
return ret;
}
std::string get_string_field(lua_State *L, const char *name, const char *line_class)
{
lua_getfield(L, -1, name);
if (!lua_isstring(L, -1))
throw bad_field("string", name, line_class);
std::string ret(lua_tostring(L, -1));
lua_pop(L, 1);
return ret;
}
double get_double_field(lua_State *L, const char *name, const char *line_class)
{
lua_getfield(L, -1, name);
@ -247,10 +257,10 @@ namespace Automation4 {
set_field(L, "fontname", sty->font);
set_field(L, "fontsize", sty->fontsize);
set_field(L, "color1", sty->primary.GetAssFormatted(true));
set_field(L, "color2", sty->secondary.GetAssFormatted(true));
set_field(L, "color3", sty->outline.GetAssFormatted(true));
set_field(L, "color4", sty->shadow.GetAssFormatted(true));
set_field(L, "color1", sty->primary.GetAssStyleFormatted() + "&");
set_field(L, "color2", sty->secondary.GetAssStyleFormatted() + "&");
set_field(L, "color3", sty->outline.GetAssStyleFormatted() + "&");
set_field(L, "color4", sty->shadow.GetAssStyleFormatted() + "&");
set_field(L, "bold", sty->bold);
set_field(L, "italic", sty->italic);
@ -305,16 +315,16 @@ namespace Automation4 {
AssEntry *result = 0;
try {
wxString section = get_string_field(L, "section", "common");
wxString section = get_wxstring_field(L, "section", "common");
if (lclass == "clear")
result = new AssEntry("", "");
else if (lclass == "comment")
result = new AssEntry(";" + get_string_field(L, "text", "comment"), section);
result = new AssEntry(";" + get_wxstring_field(L, "text", "comment"), section);
else if (lclass == "head")
result = new AssEntry(section, section);
else if (lclass == "info") {
result = new AssEntry(wxString::Format("%s: %s", get_string_field(L, "key", "info"), get_string_field(L, "value", "info")), "[Script Info]");
result = new AssEntry(wxString::Format("%s: %s", get_wxstring_field(L, "key", "info"), get_wxstring_field(L, "value", "info")), "[Script Info]");
}
else if (lclass == "format") {
// ohshi- ...
@ -324,13 +334,13 @@ namespace Automation4 {
else if (lclass == "style") {
AssStyle *sty = new AssStyle;
result = sty;
sty->name = get_string_field(L, "name", "style");
sty->font = get_string_field(L, "fontname", "style");
sty->name = get_wxstring_field(L, "name", "style");
sty->font = get_wxstring_field(L, "fontname", "style");
sty->fontsize = get_double_field(L, "fontsize", "style");
sty->primary.Parse(get_string_field(L, "color1", "style"));
sty->secondary.Parse(get_string_field(L, "color2", "style"));
sty->outline.Parse(get_string_field(L, "color3", "style"));
sty->shadow.Parse(get_string_field(L, "color4", "style"));
sty->primary = get_string_field(L, "color1", "style");
sty->secondary = get_string_field(L, "color2", "style");
sty->outline = get_string_field(L, "color3", "style");
sty->shadow = get_string_field(L, "color4", "style");
sty->bold = get_bool_field(L, "bold", "style");
sty->italic = get_bool_field(L, "italic", "style");
sty->underline = get_bool_field(L, "underline", "style");
@ -357,13 +367,13 @@ namespace Automation4 {
dia->Layer = get_int_field(L, "layer", "dialogue");
dia->Start = get_int_field(L, "start_time", "dialogue");
dia->End = get_int_field(L, "end_time", "dialogue");
dia->Style = get_string_field(L, "style", "dialogue");
dia->Actor = get_string_field(L, "actor", "dialogue");
dia->Style = get_wxstring_field(L, "style", "dialogue");
dia->Actor = get_wxstring_field(L, "actor", "dialogue");
dia->Margin[0] = get_int_field(L, "margin_l", "dialogue");
dia->Margin[1] = get_int_field(L, "margin_r", "dialogue");
dia->Margin[2] = get_int_field(L, "margin_t", "dialogue");
dia->Effect = get_string_field(L, "effect", "dialogue");
dia->Text = get_string_field(L, "text", "dialogue");
dia->Effect = get_wxstring_field(L, "effect", "dialogue");
dia->Text = get_wxstring_field(L, "text", "dialogue");
}
else {
luaL_error(L, "Found line with unknown class: %s", lclass.utf8_str().data());

View file

@ -222,7 +222,7 @@ namespace Automation4 {
bool TransferFromWindow()
{
*text = static_cast<ColourButton*>(GetWindow())->GetColour().GetAsString(wxC2S_HTML_SYNTAX);
*text = to_wx(static_cast<ColourButton*>(GetWindow())->GetColor().GetHexFormatted());
return true;
}
};
@ -247,9 +247,8 @@ namespace Automation4 {
wxControl *Create(wxWindow *parent)
{
AssColor colour;
colour.Parse(text);
wxControl *cw = new ColourButton(parent, -1, wxSize(50*width,10*height), colour.GetWXColor());
agi::Color colour(from_wx(text));
wxControl *cw = new ColourButton(parent, -1, wxSize(50*width,10*height), colour);
cw->SetValidator(ColorValidator(&text));
cw->SetToolTip(hint);
return cw;

View file

@ -233,13 +233,13 @@ void BaseGrid::UpdateStyle() {
// Set row brushes
assert(sizeof(rowColors) / sizeof(rowColors[0]) >= COLOR_LEFT_COL);
rowColors[COLOR_DEFAULT].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Background/Background")->GetColour()));
rowColors[COLOR_HEADER].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Header")->GetColour()));
rowColors[COLOR_SELECTION].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Background/Selection")->GetColour()));
rowColors[COLOR_COMMENT].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Background/Comment")->GetColour()));
rowColors[COLOR_VISIBLE].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Background/Inframe")->GetColour()));
rowColors[COLOR_SELECTED_COMMENT].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Background/Selected Comment")->GetColour()));
rowColors[COLOR_LEFT_COL].SetColour(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Left Column")->GetColour()));
rowColors[COLOR_DEFAULT].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Background/Background")->GetColor()));
rowColors[COLOR_HEADER].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Header")->GetColor()));
rowColors[COLOR_SELECTION].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Background/Selection")->GetColor()));
rowColors[COLOR_COMMENT].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Background/Comment")->GetColor()));
rowColors[COLOR_VISIBLE].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Background/Inframe")->GetColor()));
rowColors[COLOR_SELECTED_COMMENT].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Background/Selected Comment")->GetColor()));
rowColors[COLOR_LEFT_COL].SetColour(to_wx(OPT_GET("Colour/Subtitle Grid/Left Column")->GetColor()));
// Set column widths
std::vector<bool> column_array(OPT_GET("Subtitle/Grid/Column")->GetListBool());
@ -477,12 +477,12 @@ void BaseGrid::DrawImage(wxDC &dc, bool paint_columns[]) {
int maxH = (nDraw+1) * lineHeight;
// Row colors
wxColour text_standard(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Standard")->GetColour()));
wxColour text_selection(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Selection")->GetColour()));
wxColour text_collision(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Collision")->GetColour()));
wxColour text_standard(to_wx(OPT_GET("Colour/Subtitle Grid/Standard")->GetColor()));
wxColour text_selection(to_wx(OPT_GET("Colour/Subtitle Grid/Selection")->GetColor()));
wxColour text_collision(to_wx(OPT_GET("Colour/Subtitle Grid/Collision")->GetColor()));
// First grid row
wxPen grid_pen(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Lines")->GetColour()));
wxPen grid_pen(to_wx(OPT_GET("Colour/Subtitle Grid/Lines")->GetColor()));
dc.SetPen(grid_pen);
dc.DrawLine(0, 0, w, 0);
dc.SetPen(*wxTRANSPARENT_PEN);
@ -580,7 +580,7 @@ void BaseGrid::DrawImage(wxDC &dc, bool paint_columns[]) {
// Draw currently active line border
if (GetActiveLine()) {
dc.SetPen(wxPen(lagi_wxColour(OPT_GET("Colour/Subtitle Grid/Active Border")->GetColour())));
dc.SetPen(wxPen(to_wx(OPT_GET("Colour/Subtitle Grid/Active Border")->GetColor())));
dc.SetBrush(*wxTRANSPARENT_BRUSH);
int dy = (line_index_map[GetActiveLine()]+1-yPos) * lineHeight;
dc.DrawRectangle(0,dy,w,lineHeight+1);

View file

@ -363,45 +363,3 @@ void hsl_to_hsv(int iH, int iS, int iL, unsigned char *oH, unsigned char *oS, un
*oS = 2 * 255 * iS * (255 - iL) / (iL*255 + iS*255 - iL*iS);
}
}
wxString color_to_html(wxColour color)
{
return wxString::Format("#%02X%02X%02X", color.Red(), color.Green(), color.Blue());
}
wxColour html_to_color(wxString html)
{
html.Trim(true);
html.Trim(false);
if (html.StartsWith("#")) {
html.Remove(0, 1);
}
if (html.size() == 6) {
// 8 bit per channel
long r, g, b;
wxString sr, sg, sb;
sr = html.Mid(0, 2);
sg = html.Mid(2, 2);
sb = html.Mid(4, 2);
if (sr.ToLong(&r, 16) && sg.ToLong(&g, 16) && sb.ToLong(&b, 16)) {
return wxColour(r, g, b);
} else {
return wxColour(*wxBLACK);
}
} else if (html.size() == 3) {
// 4 bit per channel
long r, g, b;
wxString sr, sg, sb;
sr = html.Mid(0, 1);
sg = html.Mid(1, 1);
sb = html.Mid(2, 1);
if (sr.ToLong(&r, 16) && sg.ToLong(&g, 16) && sb.ToLong(&b, 16)) {
return wxColour(r*16+r, g*16+g, b*16+b);
} else {
return wxColour(*wxBLACK);
}
} else {
// only care about valid colors
return wxColour(*wxBLACK);
}
}

View file

@ -64,10 +64,3 @@ void rgb_to_hsv(int R, int G, int B, unsigned char *H, unsigned char *S, unsigne
void hsv_to_hsl(int iH, int iS, int iV, unsigned char *oH, unsigned char *oS, unsigned char *oL);
void hsl_to_hsv(int iH, int iS, int iL, unsigned char *oH, unsigned char *oS, unsigned char *oV);
/// Convert a wxColour to a HTML hex string
wxString color_to_html(wxColour color);
/// Convert a HTML hex string to a wxColour
wxColour html_to_color(wxString html);

View file

@ -39,41 +39,34 @@
#endif
#include "colour_button.h"
#include "config.h"
#include "compat.h"
#include "dialog_colorpicker.h"
ColourButton::ColourButton(wxWindow* parent, wxWindowID id, const wxSize& size, wxColour col)
ColourButton::ColourButton(wxWindow* parent, wxWindowID id, const wxSize& size, agi::Color col)
: wxBitmapButton(parent, id, wxBitmap(size), wxDefaultPosition, wxSize(size.GetWidth() + 6, size.GetHeight() + 6))
, bmp(GetBitmapLabel())
, colour(col)
{
{
wxMemoryDC dc;
dc.SelectObject(bmp);
dc.SetBrush(wxBrush(colour));
dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
}
Paint();
SetBitmapLabel(bmp);
Bind(wxEVT_COMMAND_BUTTON_CLICKED, &ColourButton::OnClick, this);
}
ColourButton::~ColourButton() {
void ColourButton::Paint() {
wxMemoryDC dc;
dc.SelectObject(bmp);
dc.SetBrush(wxBrush(to_wx(colour)));
dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
}
/// @brief Callback for the color picker dialog
/// @param col New color
void ColourButton::SetColour(wxColour col) {
if (!col.IsOk()) return;
void ColourButton::SetColour(agi::Color col) {
colour = col;
// Draw colour
{
wxMemoryDC dc;
dc.SelectObject(bmp);
dc.SetBrush(wxBrush(colour));
dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
}
Paint();
SetBitmapLabel(bmp);
// Trigger a click event on this as some stuff relies on that to know
@ -84,18 +77,14 @@ void ColourButton::SetColour(wxColour col) {
AddPendingEvent(evt);
}
wxColour ColourButton::GetColour() {
agi::Color ColourButton::GetColor() {
return colour;
}
/// @brief Click handler
/// @param event
void ColourButton::OnClick(wxCommandEvent &event) {
if (event.GetClientData() == this)
event.Skip();
else {
wxColour initial = colour;
if (!GetColorFromUser<ColourButton, &ColourButton::SetColour>(GetParent(), colour, this).IsOk())
SetColour(initial);
GetColorFromUser<ColourButton, &ColourButton::SetColour>(GetParent(), colour, this);
}
}

View file

@ -36,22 +36,18 @@
#include <wx/bmpbuttn.h>
#endif
#include <libaegisub/color.h>
/// DOCME
/// @class ColourButton
/// @brief DOCME
///
/// DOCME
class ColourButton: public wxBitmapButton {
private:
wxBitmap bmp; /// The button's bitmap label
wxColour colour; /// The current colour
agi::Color colour; /// The current colour
void Paint();
void OnClick(wxCommandEvent &event);
void SetColour(wxColour colour);
void SetColour(agi::Color colour);
public:
ColourButton(wxWindow* parent, wxWindowID id, const wxSize& size, wxColour col=wxColour(0,0,0));
~ColourButton();
ColourButton(wxWindow* parent, wxWindowID id, const wxSize& size, agi::Color col = agi::Color());
wxColour GetColour();
agi::Color GetColor();
};

View file

@ -302,19 +302,17 @@ void toggle_override_tag(const agi::Context *c, bool (AssStyle::*field), const c
commit_text(c, undo_msg, sel_start, sel_end);
}
void got_color(const agi::Context *c, const char *tag, int *commit_id, wxColour new_color) {
if (new_color.Ok()) {
int sel_start = c->textSelectionController->GetSelectionStart();
int sel_end = c->textSelectionController->GetSelectionEnd();
set_tag(c, tag, AssColor(new_color).GetAssFormatted(false), sel_start, sel_end);
commit_text(c, _("set color"), sel_start, sel_end, commit_id);
}
void got_color(const agi::Context *c, const char *tag, int *commit_id, agi::Color new_color) {
int sel_start = c->textSelectionController->GetSelectionStart();
int sel_end = c->textSelectionController->GetSelectionEnd();
set_tag(c, tag, new_color.GetAssOverrideFormatted(), sel_start, sel_end);
commit_text(c, _("set color"), sel_start, sel_end, commit_id);
}
void show_color_picker(const agi::Context *c, AssColor (AssStyle::*field), const char *tag, const char *alt) {
void show_color_picker(const agi::Context *c, agi::Color (AssStyle::*field), const char *tag, const char *alt) {
AssDialogue *const line = c->selectionController->GetActiveLine();
AssStyle const* const style = c->ass->GetStyle(line->Style);
wxColor color = (style ? style->*field : AssStyle().*field).GetWXColor();
agi::Color color = (style ? style->*field : AssStyle().*field);
line->ParseAssTags();
@ -324,11 +322,11 @@ void show_color_picker(const agi::Context *c, AssColor (AssStyle::*field), const
color = get_value(*line, blockn, color, tag, alt);
int commit_id = -1;
const wxColor newColor = GetColorFromUser(c->parent, color, bind(got_color, c, tag, &commit_id, std::tr1::placeholders::_1));
bool ok = GetColorFromUser(c->parent, color, bind(got_color, c, tag, &commit_id, std::tr1::placeholders::_1));
line->ClearBlocks();
commit_text(c, _("set color"), -1, -1, &commit_id);
if (!newColor.IsOk()) {
if (!ok) {
c->ass->Undo();
c->textSelectionController->SetSelection(sel_start, sel_end);
}

View file

@ -6,10 +6,15 @@
#include <wx/string.h>
#endif
#include <libaegisub/colour.h>
#include <libaegisub/color.h>
#define STD_STR(x) std::string((x).utf8_str())
inline wxColour lagi_wxColour(const agi::Colour &colour) { return wxColour(colour); }
inline wxColour to_wx(agi::Color color) { return wxColour(color.r, color.g, color.b, 255 - color.a); }
inline wxString to_wx(std::string const& str) { return wxString(str.c_str(), wxConvUTF8); }
inline agi::Color from_wx(wxColour color) { return agi::Color(color.Red(), color.Green(), color.Blue(), 255 - color.Alpha()); }
inline std::string from_wx(wxString const& str) { return std::string(str.utf8_str()); }
inline wxString lagi_wxString(const std::string &str) { return wxString(str.c_str(), wxConvUTF8); }
wxArrayString lagi_MRU_wxAS(const wxString &list);

View file

@ -77,11 +77,6 @@
#include <ApplicationServices/ApplicationServices.h>
#endif
/// DOCME
/// @class ColorPickerSpectrum
/// @brief DOCME
///
/// DOCME
class ColorPickerSpectrum : public wxControl {
public:
enum PickerDirection {
@ -93,10 +88,7 @@ private:
int x;
int y;
/// DOCME
wxBitmap *background;
/// DOCME
PickerDirection direction;
void OnPaint(wxPaintEvent &evt);
@ -113,18 +105,15 @@ public:
void SetBackground(wxBitmap *new_background, bool force = false);
};
/// DOCME
/// @class ColorPickerRecent
/// @brief DOCME
///
/// DOCME
/// @brief A grid of recently used colors which can be selected by clicking on them
class ColorPickerRecent : public wxControl {
int rows; ///< Number of rows of colors
int cols; ///< Number of cols of colors
int cellsize; ///< Width/Height of each cell
/// The colors currently displayed in the control
std::vector<wxColour> colors;
std::vector<agi::Color> colors;
/// Does the background need to be regenerated?
bool background_valid;
@ -141,29 +130,20 @@ class ColorPickerRecent : public wxControl {
public:
ColorPickerRecent(wxWindow *parent, int cols, int rows, int cellsize);
/// Load the colors to show from a string
void LoadFromString(const wxString &recent_string = wxString());
/// Save the colors currently shown to a string
wxString StoreToString();
/// Load the colors to show
void Load(std::vector<agi::Color> const& recent_colors);
/// Get the list of recent colors
std::vector<agi::Color> Save() const;
/// Add a color to the beginning of the recent list
void AddColor(wxColour color);
void AddColor(agi::Color color);
};
/// DOCME
/// @class ColorPickerScreenDropper
/// @brief DOCME
///
/// DOCME
class ColorPickerScreenDropper : public wxControl {
/// DOCME
wxBitmap capture;
/// DOCME
/// DOCME
int resx, resy;
/// DOCME
int magnification;
void OnMouse(wxMouseEvent &evt);
@ -177,15 +157,10 @@ public:
void DropFromScreenXY(int x, int y);
};
/// DOCME
/// @class DialogColorPicker
/// @brief DOCME
///
/// DOCME
class DialogColorPicker : public wxDialog {
agi::scoped_ptr<PersistLocation> persist;
wxColour cur_color; ///< Currently selected colour
agi::Color cur_color; ///< Currently selected colour
bool spectrum_dirty; ///< Does the spectrum image need to be regenerated?
ColorPickerSpectrum *spectrum; ///< The 2D color spectrum
@ -240,7 +215,7 @@ class DialogColorPicker : public wxDialog {
/// Update all other controls as a result of modifying the HTML format control
void UpdateFromHTML();
void SetRGB(unsigned char r, unsigned char g, unsigned char b);
void SetRGB(agi::Color new_color);
void SetHSL(unsigned char r, unsigned char g, unsigned char b);
void SetHSV(unsigned char r, unsigned char g, unsigned char b);
@ -260,22 +235,21 @@ class DialogColorPicker : public wxDialog {
void OnChangeMode(wxCommandEvent &evt);
void OnSpectrumChange(wxCommandEvent &evt);
void OnSliderChange(wxCommandEvent &evt);
void OnRecentSelect(wxCommandEvent &evt); // also handles dropper pick
void OnRecentSelect(wxThreadEvent &evt); // also handles dropper pick
void OnDropperMouse(wxMouseEvent &evt);
void OnMouse(wxMouseEvent &evt);
void OnCaptureLost(wxMouseCaptureLostEvent&);
std::tr1::function<void (wxColour)> callback;
std::tr1::function<void (agi::Color)> callback;
public:
DialogColorPicker(wxWindow *parent, wxColour initial_color, std::tr1::function<void (wxColour)> callback);
DialogColorPicker(wxWindow *parent, agi::Color initial_color, std::tr1::function<void (agi::Color)> callback);
~DialogColorPicker();
void SetColor(wxColour new_color);
wxColour GetColor();
void SetColor(agi::Color new_color);
agi::Color GetColor();
};
/// DOCME
static const int spectrum_horz_vert_arrow_size = 4;
ColorPickerSpectrum::ColorPickerSpectrum(wxWindow *parent, PickerDirection direction, wxSize size)