Screen space color retrieval by ruby

Hi all,
I searched a bit about this but couldn’t find anything to get me started.
Is there any way to get the color of x pixel at coordinates U,V in screen space in a ruby script?
Thank you in advance for any info.
Have a great day.

You can use the view.write_image method with the source from :frame_buffer to capture the current view as an image. After that, employ ImageRef to load this image and retrieve the color value at the specific U and V coordinates. This approach will allow you to determine the color of the pixel you’re interested in within the screen space.

class SimpleColorPickerTool
  def initialize
    @image_path = File.join(Sketchup.temp_dir, 'example.png')
  end

  def onLButtonDown(flags, x, y, view)
    # Capture the current view into an image
    options = {
      :filename => @image_path,
      :source => :framebuffer,
      :compression => 0.9
    }
    view.write_image(options)

    # Load the image and get the color at the click coordinates
    image = Sketchup::ImageRep.new
    image.load_file(@image_path)

    u = x.to_f / view.vpwidth
    v = y.to_f / view.vpheight
    color = image.color_at_uv(u, v, true)

    UI.messagebox("Color at (#{x}, #{y}): #{color}")
  end
end

# To use this tool, activate it with:
Sketchup.active_model.select_tool(SimpleColorPickerTool.new)

Thank you for the idea.
Is this method fast enough?
I will probably batch process these and I am not sure writing images and getting them back will be possible in reasonable times.
Is there any way to access the frame_buffer directly?
Best regards

Currently, the only way to access the frame_buffer is through the ‘write image’ method. This is the fastest approach I’m aware of for exporting images from the viewport.

Thank you again.
Got it, I will try it.
Have a great day