Implement color conversion for dark SVG icons

This commit is contained in:
toger5 2017-08-30 20:29:35 +02:00
parent 426c6b2c49
commit aa8feff8c1
4 changed files with 123 additions and 41 deletions

View file

@ -89,14 +89,16 @@ static Ref<StyleBoxFlat> change_border_color(Ref<StyleBoxFlat> p_style, Color p_
#define HIGHLIGHT_COLOR_LIGHT highlight_color.linear_interpolate(Color(1, 1, 1, 1), 0.3)
#define HIGHLIGHT_COLOR_DARK highlight_color.linear_interpolate(Color(0, 0, 0, 1), 0.5)
Ref<ImageTexture> editor_generate_icon(int p_index, bool dark_theme = true) {
Ref<ImageTexture> editor_generate_icon(int p_index, bool dark_theme = true, Dictionary *p_colors = NULL) {
Ref<ImageTexture> icon = memnew(ImageTexture);
Ref<Image> img = memnew(Image);
// dumb gizmo check
bool is_gizmo = String(editor_icons_names[p_index]).begins_with("Gizmo");
ImageLoaderSVG::create_image_from_string(img, dark_theme ? editor_icons_sources[p_index] : editor_icons_sources_dark[p_index], EDSCALE, true);
ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], EDSCALE, true, dark_theme ? NULL : p_colors);
if ((EDSCALE - (float)((int)EDSCALE)) > 0.0 || is_gizmo)
icon->create_from_image(img); // in this case filter really helps
else
@ -105,19 +107,74 @@ Ref<ImageTexture> editor_generate_icon(int p_index, bool dark_theme = true) {
return icon;
}
void editor_register_icons(Ref<Theme> p_theme, bool dark_theme = true) {
#ifndef ADD_CONVERT_COLOR
#define ADD_CONVERT_COLOR(dictionary, old_color, new_color) dictionary[Color::html(old_color)] = Color::html(new_color)
#endif
void editor_register_and_generate_icons(Ref<Theme> p_theme, bool dark_theme = true) {
#ifdef SVG_ENABLED
print_line(rtos(EDSCALE));
Dictionary dark_icon_color_dictionary;
//convert color: FROM TO
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#e0e0e0", "#4f4f4f"); // common icon color
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ffffff", "#000000"); // white
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#b4b4b4", "#000000"); // script darker color
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#cea4f1", "#bb6dff"); // animation
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#fc9c9c", "#ff5f5f"); // spatial
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#a5b7f3", "#6d90ff"); // 2d
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#708cea", "#0843ff"); // 2d dark
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#a5efac", "#29d739"); // control
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ff8484", "#ff3333"); // error
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#84ffb1", "#00db50"); // success
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ffd684", "#ffad07"); // warning
// rainbow
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ff7070", "#ff2929"); // red
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ffeb70", "#ffe337"); // yellow
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#9dff70", "#74ff34"); // green
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#70ffb9", "#2cff98"); // aqua
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#70deff", "#22ccff"); // blue
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#9f70ff", "#702aff"); // purple
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ff70ac", "#ff2781"); // pink
// audio gradient
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ff8484", "#ff4040"); // red
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#e1dc7a", "#d6cf4b"); // yellow
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#84ffb1", "#00f010"); // green
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ffd684", "#fea900"); // mesh (orange)
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#40a2ff", "#68b6ff"); // shape (blue)
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#84c2ff", "#5caeff"); // selection (blue)
ADD_CONVERT_COLOR(dark_icon_color_dictionary, "#ea686c", "#e3383d"); // key xform (red)
List<String> exceptions;
exceptions.push_back("EditorPivot");
exceptions.push_back("EditorHandle");
exceptions.push_back("Editor3DHandle");
exceptions.push_back("Godot");
exceptions.push_back("PanoramaSky");
exceptions.push_back("ProceduralSky");
exceptions.push_back("EditorControlAnchor");
exceptions.push_back("DefaultProjectIcon");
clock_t begin_time = clock();
for (int i = 0; i < editor_icons_count; i++) {
Ref<ImageTexture> icon = editor_generate_icon(i, dark_theme);
List<String>::Element *is_exception = exceptions.find(editor_icons_names[i]);
if (is_exception) {
exceptions.erase(is_exception);
}
Ref<ImageTexture> icon = editor_generate_icon(i, dark_theme, is_exception ? NULL : &dark_icon_color_dictionary);
p_theme->set_icon(editor_icons_names[i], "EditorIcons", icon);
}
clock_t end_time = clock();
double time_d = (double)(end_time - begin_time) / CLOCKS_PER_SEC;
print_line("SVG_GENERATION TIME: " + rtos(time_d));
#else

View file

@ -10,42 +10,25 @@ def make_editor_icons_action(target, source, env):
dst = target[0].srcnode().abspath
svg_icons = source
whites = StringIO()
darks = StringIO()
icons_string = StringIO()
for f in svg_icons:
fname = str(f)
whites.write('\t"')
darks.write('\t"')
icons_string.write('\t"')
with open(fname, 'rb') as svgf:
b = svgf.read(1)
while(len(b) == 1):
whites.write("\\" + str(hex(ord(b)))[1:])
icons_string.write("\\" + str(hex(ord(b)))[1:])
b = svgf.read(1)
try:
with open(os.path.dirname(fname) + "/dark/" + os.path.basename(fname), 'rb') as svgf:
b = svgf.read(1)
while(len(b) == 1):
darks.write("\\" + str(hex(ord(b)))[1:])
b = svgf.read(1)
except IOError:
with open(fname, 'rb') as svgf:
b = svgf.read(1)
while(len(b) == 1):
darks.write("\\" + str(hex(ord(b)))[1:])
b = svgf.read(1)
whites.write('"')
darks.write('"')
icons_string.write('"')
if fname != svg_icons[-1]:
whites.write(",")
darks.write(",")
whites.write('\n')
darks.write('\n')
icons_string.write(",")
icons_string.write('\n')
s = StringIO()
s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
@ -53,10 +36,7 @@ def make_editor_icons_action(target, source, env):
s.write("#define _EDITOR_ICONS_H\n")
s.write("static const int editor_icons_count = %s;\n" % len(svg_icons))
s.write("static const char *editor_icons_sources[] = {\n")
s.write(whites.getvalue())
s.write('};\n\n')
s.write("static const char *editor_icons_sources_dark[] = {\n")
s.write(darks.getvalue())
s.write(icons_string.getvalue())
s.write('};\n\n')
s.write("static const char *editor_icons_names[] = {\n")
for f in svg_icons:
@ -78,8 +58,7 @@ def make_editor_icons_action(target, source, env):
f.write(s.getvalue())
f.close()
s.close()
whites.close()
darks.close()
icons_string.close()
make_editor_icons_builder = Builder(action=make_editor_icons_action,
suffix='.h',

View file

@ -47,7 +47,49 @@ SVGRasterizer::~SVGRasterizer() {
SVGRasterizer ImageLoaderSVG::rasterizer;
Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample) {
inline void change_nsvg_paint_color(NSVGpaint *p_paint, const uint32_t p_old, const uint32_t p_new) {
if (p_paint->type == NSVG_PAINT_COLOR) {
if (p_paint->color << 8 == p_old << 8) {
p_paint->color = p_new;
}
}
if (p_paint->type == NSVG_PAINT_LINEAR_GRADIENT || p_paint->type == NSVG_PAINT_RADIAL_GRADIENT) {
for (int stop_index = 0; stop_index < p_paint->gradient->nstops; stop_index++) {
if (p_paint->gradient->stops[stop_index].color << 8 == p_old << 8) {
p_paint->gradient->stops[stop_index].color = p_new;
}
}
}
}
void ImageLoaderSVG::_convert_colors(NSVGimage *p_svg_image, const Dictionary p_colors) {
List<uint32_t> replace_colors_i;
List<uint32_t> new_colors_i;
List<Color> replace_colors;
List<Color> new_colors;
for (int i = 0; i < p_colors.keys().size(); i++) {
Variant r_c = p_colors.keys()[i];
Variant n_c = p_colors[p_colors.keys()[i]];
if (r_c.get_type() == Variant::COLOR && n_c.get_type() == Variant::COLOR) {
Color replace_color = r_c;
Color new_color = n_c;
replace_colors_i.push_back(replace_color.to_ABGR32());
new_colors_i.push_back(new_color.to_ABGR32());
}
}
for (NSVGshape *shape = p_svg_image->shapes; shape != NULL; shape = shape->next) {
for (int i = 0; i < replace_colors_i.size(); i++) {
change_nsvg_paint_color(&(shape->stroke), replace_colors_i[i], new_colors_i[i]);
change_nsvg_paint_color(&(shape->fill), replace_colors_i[i], new_colors_i[i]);
}
}
}
Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample, const Dictionary *p_replace_colors) {
NSVGimage *svg_image;
PoolVector<uint8_t>::Read src_r = p_data->read();
svg_image = nsvgParse((char *)src_r.ptr(), "px", 96);
@ -56,6 +98,9 @@ Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t
return ERR_FILE_CORRUPT;
}
if (p_replace_colors != NULL) {
_convert_colors(svg_image, *p_replace_colors);
}
float upscale = upsample ? 2.0 : 1.0;
int w = (int)(svg_image->width * p_scale * upscale);
@ -78,7 +123,7 @@ Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t
return OK;
}
Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *svg_str, float p_scale, bool upsample) {
Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *svg_str, float p_scale, bool upsample, const Dictionary *p_replace_colors) {
size_t str_len = strlen(svg_str);
PoolVector<uint8_t> src_data;
@ -86,7 +131,7 @@ Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *s
PoolVector<uint8_t>::Write src_w = src_data.write();
memcpy(src_w.ptr(), svg_str, str_len + 1);
return _create_image(p_image, &src_data, p_scale, upsample);
return _create_image(p_image, &src_data, p_scale, upsample, p_replace_colors);
}
Error ImageLoaderSVG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {

View file

@ -54,14 +54,15 @@ public:
class ImageLoaderSVG : public ImageFormatLoader {
static SVGRasterizer rasterizer;
static Error _create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample);
static void _convert_colors(NSVGimage *p_svg_imge, const Dictionary p_colors);
static Error _create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample, const Dictionary *p_replace_color = NULL);
public:
static Error create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale, bool upsample);
static Error create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale, bool upsample, const Dictionary *p_replace_color = NULL);
virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderSVG();
};
#endif
#endif