You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-22 15:06:45 +00:00
Implement color conversion for dark SVG icons
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user