From 33e41662a4ffa476cd74f492851b27bffcae768d Mon Sep 17 00:00:00 2001
From: Pim Hakkert
Date: Sun, 21 Jul 2024 15:58:33 +0200
Subject: [PATCH] Added a function to get the color (RGBA) of a specific pixel
by coordinates
---
addons/gdcef/doc/API.md | 3 ++-
addons/gdcef/gdcef/src/gdbrowser.cpp | 18 ++++++++++++++++++
addons/gdcef/gdcef/src/gdbrowser.hpp | 6 ++++++
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/addons/gdcef/doc/API.md b/addons/gdcef/doc/API.md
index d16a19c..34db7fd 100644
--- a/addons/gdcef/doc/API.md
+++ b/addons/gdcef/doc/API.md
@@ -19,7 +19,8 @@ Chromium Embedded Framework. This class can create instances of `GDBrowserView`
| `_process` | `dt`: float | void | Hidden function called automatically by Godot and call CEF internal pump messages. `dt` is not used. |
| `create_browser` | `url`: string, `texture_rect`: TextureRect, `settings`: godot::Dictionary | GDBrowserView* | Create a browser tab and store its instance as child node. `url`: the page link. `texture_rect`: the container holding the texture. `settings` optional settings for the created browser. |
| `shutdown` | | | Release CEF memory and sub CEF processes are notified that the application is exiting. All browsers are destroyed. `GDCef` is no more alive. |
-| `get_error` | | String | Return the latest error. |
+| `get_pixel_color` | `x`: int, `y`: int | Color | Returns the color of a specific pixel in the browser view.
+| `get_error` | | String | Return the latest error. |
| `get_version` | | String | Return the full CEF version as string. |
| `get_version_part` | `entry`: int | int | Return part of the CEF version as integer. |
diff --git a/addons/gdcef/gdcef/src/gdbrowser.cpp b/addons/gdcef/gdcef/src/gdbrowser.cpp
index 959dfa7..a7c9ed4 100644
--- a/addons/gdcef/gdcef/src/gdbrowser.cpp
+++ b/addons/gdcef/gdcef/src/gdbrowser.cpp
@@ -111,6 +111,7 @@ void GDBrowserView::_bind_methods()
ClassDB::bind_method(D_METHOD("is_muted"), &GDBrowserView::muted);
ClassDB::bind_method(D_METHOD("set_audio_stream", "audio"), &GDBrowserView::setAudioStreamer);
ClassDB::bind_method(D_METHOD("get_audio_stream"), &GDBrowserView::getAudioStreamer);
+ ClassDB::bind_method(D_METHOD("get_pixel_color", "x", "y"), &GDBrowserView::getPixelColor);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_NODE_TYPE,
"AudioStreamGeneratorPlayback"), "set_audio_stream", "get_audio_stream");
@@ -683,4 +684,21 @@ void GDBrowserView::onAudioStreamPacket(CefRefPtr browser,
streamer.push_frame(godot::Vector2(data[0][i], data[0][i]));
}
}
+}
+
+//------------------------------------------------------------------------------
+godot::Color GDBrowserView::getPixelColor(int x, int y) const
+{
+ // Ensure the browser is valid and coordinates are within bounds
+ if (x < 0 || y < 0 || x >= m_width || y >= m_height || m_data.size() == 0)
+ return godot::Color(1, 1, 1, 1); // Return full white as fallback
+
+
+ int index = (y * m_width + x) * 4;
+ unsigned char r = m_data[index + 0];
+ unsigned char g = m_data[index + 1];
+ unsigned char b = m_data[index + 2];
+ unsigned char a = m_data[index + 3];
+
+ return godot::Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
}
\ No newline at end of file
diff --git a/addons/gdcef/gdcef/src/gdbrowser.hpp b/addons/gdcef/gdcef/src/gdbrowser.hpp
index 4c3933c..efc843c 100644
--- a/addons/gdcef/gdcef/src/gdbrowser.hpp
+++ b/addons/gdcef/gdcef/src/gdbrowser.hpp
@@ -547,6 +547,12 @@ class GDBrowserView : public godot::Node
return nullptr;
return m_impl->m_audio.streamer;
}
+
+ // -------------------------------------------------------------------------
+ //! \brief Exported method to Godot script. Get the color of the currently
+ //! hovered on pixel
+ // -------------------------------------------------------------------------
+ godot::Color getPixelColor(int x, int y) const;
private: