Skip to content

Commit

Permalink
Added a function to get the color (RGBA) of a specific pixel by coord…
Browse files Browse the repository at this point in the history
…inates
  • Loading branch information
pimhakkert authored and Lecrapouille committed Jul 23, 2024
1 parent a02c79d commit 33e4166
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion addons/gdcef/doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
18 changes: 18 additions & 0 deletions addons/gdcef/gdcef/src/gdbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -683,4 +684,21 @@ void GDBrowserView::onAudioStreamPacket(CefRefPtr<CefBrowser> 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);
}
6 changes: 6 additions & 0 deletions addons/gdcef/gdcef/src/gdbrowser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 33e4166

Please sign in to comment.