Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to Convert HSV to RGB
- JEF3275
-
14 posts
How to Convert HSV to RGB
Hello, I am creating a ray marcher in scratch, and I want objects to have mathematically correct reflections. To do this, I need to be able to blend between colors of the original object and other objects that can be seen within the reflections on this first object. I have done some research and found that the best way to blend colors is to first convert them to RGB, and average the values of the colors you want to blend.
The Scratch wiki has a method for setting the pen color by using an RGB value here: https://en.scratch-wiki.info/wiki/Computer_Colors , but this is of no use to me if I am not able to generate an RGB color in the first place.
I have not been able to find a way to convert the three Hue, Saturation, and Brightness values into the three Red, Green, and Blue values.
Is there a straightforward way I could create a custom block with three number inputs for HSV that would convert them into three variables for RGB?
For example, if Hue = 0, Saturation = 100, Brightness = 100, then the RGB equivalent would be Red = 255, Green = 0, Blue = 0 (I think)
Thanks
The Scratch wiki has a method for setting the pen color by using an RGB value here: https://en.scratch-wiki.info/wiki/Computer_Colors , but this is of no use to me if I am not able to generate an RGB color in the first place.
I have not been able to find a way to convert the three Hue, Saturation, and Brightness values into the three Red, Green, and Blue values.
Is there a straightforward way I could create a custom block with three number inputs for HSV that would convert them into three variables for RGB?
For example, if Hue = 0, Saturation = 100, Brightness = 100, then the RGB equivalent would be Red = 255, Green = 0, Blue = 0 (I think)
Thanks
Last edited by JEF3275 (June 29, 2023 18:22:06)
- Link2358
-
100+ posts
How to Convert HSV to RGB
This is something I found on the web, I don't know about hsv to rgb, so yeah:

I also got ChatGPT to write up on this:

I also got ChatGPT to write up on this:
Sorry if this is a bit lacking. I personally don't know ANYTHING about the subject. .-.
- Normalize the hue value to the range (0, 1) by dividing it by 360: H' = H / 360.
- Calculate the chroma (C) by multiplying the saturation (S) and the value (V): C = S * V.
- Calculate the X value using the formula: X = C * (1 - abs((H' * 6) mod 2 - 1)).
- Calculate the intermediate values R1, G1, and B1 based on the hue value range:
If 0 ≤ H' < 1: R1 = C, G1 = X, B1 = 0.
If 1 ≤ H' < 2: R1 = X, G1 = C, B1 = 0.
If 2 ≤ H' < 3: R1 = 0, G1 = C, B1 = X.
If 3 ≤ H' < 4: R1 = 0, G1 = X, B1 = C.
If 4 ≤ H' < 5: R1 = X, G1 = 0, B1 = C.
If 5 ≤ H' < 6: R1 = C, G1 = 0, B1 = X.- Calculate the values R', G', and B' by adding the chroma (C) to the intermediate values:
R' = (R1 + (V - C)) * 255
G' = (G1 + (V - C)) * 255
B' = (B1 + (V - C)) * 255.- Clamp the R', G', and B' values to the range (0, 255).
Last edited by Link2358 (June 29, 2023 18:56:16)
- JEF3275
-
14 posts
How to Convert HSV to RGB
Thank you so much! I was able to implement the formula and it works perfectly.
For anyone else that wants to change colors from HSV to RGB and set the pen color using RGB here is a little demo I made: https://scratch.mit.edu/projects/870680727/
For anyone else that wants to change colors from HSV to RGB and set the pen color using RGB here is a little demo I made: https://scratch.mit.edu/projects/870680727/
Last edited by JEF3275 (June 29, 2023 20:47:39)
- Bri1079
-
17 posts
How to Convert HSV to RGB
Thank you so much! I was able to implement the formula and it works perfectly.
For anyone else that wants to change colors from HSV to RGB and set the pen color using RGB here is a little demo I made: https://scratch.mit.edu/projects/870680727/
is the project still up?
- Discussion Forums
- » Help with Scripts
-
» How to Convert HSV to RGB