Discuss Scratch

DiamondCrewmate
Scratcher
19 posts

(Solved) Trying to do RGB color mixing.

I'm trying to mix RGB values but I can't find out how to make it work.
I need to either mix combined RGB values (as in 1 number values, the unreadable kind of RGB) or separated channel RGB (as in the different red green and blue values)
the colors that I have are in combined RGB format. so a fully white pixel is color: “16777215” when I want either #ffffff or 255,255,255.
I preferably need a way to convert combined RGB to separate RGB channels
Any help would be greatly appreciated.

Last edited by DiamondCrewmate (Sept. 29, 2024 13:03:47)

Neon_Sphere
Scratcher
9 posts

(Solved) Trying to do RGB color mixing.

So you have a pretty complicated request here. The formula for converting one combined RGB color to individual channels is:

color = (red * 256 * 256) + (green * 256) + (blue)

I thought that it was not possible to reverse this process, but according to griffpatch:

red = floor(color / (256 * 256))
green = floor(color / 256) mod 256
blue = color mod 256

It looks like you are wanting to mix RGB colors together in your project, but this is very complicated. You can try averaging the RBG values, but it won't work in all instances. Anyway, hope these formulas help!

If you want to know more info about colors in Scratch, check out Griffpatch's video, “How to GET COLOR at point”. That is where I found these formulas.

Last edited by Neon_Sphere (Sept. 29, 2024 11:45:25)

DiamondCrewmate
Scratcher
19 posts

(Solved) Trying to do RGB color mixing.

k thank you very much!
Raiyan2
Scratcher
4 posts

(Solved) Trying to do RGB color mixing.

If it helps you, you can combine multiple RGB colors by setting the pen color to the combined RGB value of the first color, and transparency to 0. Then use pen down, set the color to the combined RGB value of the second color, and a transparency of 50, then use pen down. This mixes the colors more accurately than using averages.
awesome-llama
Scratcher
1000+ posts

(Solved) Trying to do RGB color mixing.

Neon_Sphere wrote:

It looks like you are wanting to mix RGB colors together in your project, but this is very complicated. You can try averaging the RBG values, but it won't work in all instances.

It depends. The important thing is that mixing works best on the individual colour channels, i.e. you will not get a decent result by trying to mix the combined values.

Averaging is an option, like you said.
On each of the channels, add the two values and divide by 2. Make sure to round the result if intending to recombine the channels.

set [averaged red channel v] to (round (((color 1 red channel) + (color 2 red channel)) / (2))) // do this for all the channels

But what if you don't want the average which is at the middle of the two colours? Maybe you want different proportion of the two? This is where linear interpolation comes in.
The proportion of the mix is controlled by a number between 0 and 1, with either end being using one of the colours only and 0.5 being the equivalent of an average (use an equal amount of each colour). This value is often given the variable named t.
To implement this in Scratch, you can do the following:

set [mixed red channel v] to (round ((color 1 red channel) + (((color 2 red channel) - (color 1 red channel)) * (t))))
When t=0, use the first colour only. When t=1, use the second colour only. Any other value of t is using some mix of both.



You should find the above sufficient for most use cases. You may ignore what I am writing below…

It can get more complicated when you want it to be more perceptually uniform, in this case, convert the colour into channels of a more perceptually uniform space like Oklab. Or at a minimum undo the gamma correction applied to sRGB so the channels are linear again.



Policy on “necroposted” topics

Last edited by awesome-llama (Yesterday 02:16:14)

Powered by DjangoBB