diff --git a/core/image.cpp b/core/image.cpp index 51fbe75decd1..3586c501aaab 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -1868,6 +1868,53 @@ void Image::fill(const Color &c) { unlock(); } +void Image::replace_color(const Color &p_color_old, const Color &p_color_new) { + + if (p_color_old != p_color_new) { + + uint32_t pixel_size = get_format_pixel_size(format); + + uint8_t colorb_new[16]; + uint8_t colorb_old[16]; + uint8_t colorb_current[16]; + + Image new_img(2, 1, 0, format); + new_img.lock(); + + new_img.set_pixel(0, 0, p_color_new); + new_img.set_pixel(1, 0, p_color_old); + + PoolVector::Read r_dummy = new_img.data.read(); + new_img._get_pixelb(0, 0, pixel_size, r_dummy.ptr(), colorb_new); + new_img._get_pixelb(1, 0, pixel_size, r_dummy.ptr(), colorb_old); + + new_img.unlock(); + + lock(); + + PoolVector::Read r = data.read(); + PoolVector::Write w = data.write(); + + bool found = false; + for (int y = 0; y < height; y++) { + + for (int x = 0; x < width; x++) { + found = true; + _get_pixelb(x, y, pixel_size, r.ptr(), colorb_current); + for (uint32_t i = 0; i < pixel_size; i++) + if (colorb_current[i] != colorb_old[i]) { + found = false; + } + if (found) { + _put_pixelb(x, y, pixel_size, w.ptr(), colorb_new); + } + } + } + + unlock(); + } +} + Ref (*Image::_png_mem_loader_func)(const uint8_t *, int) = NULL; Ref (*Image::_jpg_mem_loader_func)(const uint8_t *, int) = NULL; @@ -2308,6 +2355,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect); ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask); ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill); + ClassDB::bind_method(D_METHOD("replace_color", "color_old", "color_new"), &Image::replace_color); ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect); ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect); diff --git a/core/image.h b/core/image.h index 80a0c339dda8..86b43729fe4c 100644 --- a/core/image.h +++ b/core/image.h @@ -291,6 +291,7 @@ class Image : public Resource { void blend_rect(const Ref &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); void blend_rect_mask(const Ref &p_src, const Ref &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); void fill(const Color &c); + void replace_color(const Color &color_old, const Color &color_new); Rect2 get_used_rect() const; Ref get_rect(const Rect2 &p_area) const;