How to get pixels' color in RGB and show in from 0~255?
Asked Answered
D

4

0

Hello. I want to learn something about getting pixels' colors where under the mouse cursor position. I need it to return the RGB value of the color of where my mouse is on and then retrieving the value corresponding to this RGB (as a key) with the dictionary.

First step I need to get the color of pixels where my mouse's position is. So I use:

> var mouse_position =get_viewpoint().get_mouse_position
> var mouse_position_color =get_pixel ( int (mouse_position.x), int (mouse_position).y )
> print(mous_position)
> print(mous_position_color)

By doing this, I get the value of variable "mouse_position_color" in floating point decimal form, e.g. (0.01000, 0.4004, 0.6555, 1). Personally, I'm uncomfortable with this form of expression, I'd like it to be expressed as an integer value from 0 to 255, such as (100, 125, 218, 1).

Can someone tell me how to achieve it? Thank you all very much.

Duodenitis answered 22/12, 2023 at 3:4 Comment(0)
E
0

Duodenitis Just multiply each component with 255. 0.4004 would become 102.

Or if you already have a Color object: prints(color.r8, color.g8, color.b8)

https://docs.godotengine.org/en/latest/classes/class_color.html#properties

Evalynevan answered 22/12, 2023 at 13:43 Comment(0)
D
0

Evalynevan Just multiply each component with 255. 0.4004 would become 102.

Thank you.

I don't know in details about how to multiply each component, do you mean write the GDScript like this:
var mouse_position_color =get_pixel ( int (mouse_position.x), int (mouse_position).y )*255?

Duodenitis answered 27/12, 2023 at 2:11 Comment(0)
E
0

Duodenitis Color object already has properties that hold 8 bit component representation. Look at the reference for Color class.

print(color.r8, ", ", color.g8, ", ", color.b8, ", ", color.a8)
Eggett answered 27/12, 2023 at 9:14 Comment(0)
W
0

Duodenitis how to multiply each component,

To convert a float F in the range [0.0, 1.0] to an int I in the range [0, 255]:
I = roundi(F * 255.0)

Wulfila answered 27/12, 2023 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.