Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Converting Colors to RGB
- TestCorps
-
Scratcher
2 posts
Converting Colors to RGB
So I am making A drawing engine.
It Involves RGB into Scratch color code(a.k.a. RGBA). the problem is, how do I convert colors(I mean HSB.) to RGB or to convert colors into
color code(RGBA)? (I would also like to know, how does the scratch color code work anyways?)
Here is the most important element:
It Involves RGB into Scratch color code(a.k.a. RGBA). the problem is, how do I convert colors(I mean HSB.) to RGB or to convert colors into
color code(RGBA)? (I would also like to know, how does the scratch color code work anyways?)
Here is the most important element:
((((r) * (65536)) + ((g) * (256))) + (b))there is a way for you to enter separate r,g,b,and a, but that doesn't work.
Last edited by TestCorps (Jan. 12, 2021 22:47:09)
- dimitrip
-
Scratcher
500+ posts
Converting Colors to RGB
Hi,
You probably saw it already, Scratch! pen colors work this way.
Now, given a Scratch! color value, how to find back the r (red), g (green) and b (blue) quantities?
Well you'll remark in your post, that the red quantity is multiplied by 65536 and summed in the color value computation.
So if you divide the color value by 65536, and take the floor of the result, you'll find back the r value (between 0 and 255).
That is, r = floor(color_value / 65536)
Now that you know the r value, you can remove its influence in the color value, by computing the remainder_gb.
remainder_gb = color_value - 65536 * r
Now you'll also remark in your post, that the green quantity is multiplied by 256 in the color value's computation.
So if you divide the remainder by 256, and again take the floor of the result, you'll find back the g value (also between 0 and 255).
That is, g = floor(remainder_gb / 256)
Good, you now also know the g value. You can remove its influence in the color value, by computing the remainder_b.
remainder_b = remainder_gb - 256 * g
Well, remainder_b… is the blue (b) value (again between 0 and 255)
Notice: A Scratch! color may also have a certain transparency.
To neglect this aspect and just find r, g and b accurately, you should first do this:
color_value = color_value modulo 16777216.
Greetz,
Dimitri
You probably saw it already, Scratch! pen colors work this way.
Now, given a Scratch! color value, how to find back the r (red), g (green) and b (blue) quantities?
Well you'll remark in your post, that the red quantity is multiplied by 65536 and summed in the color value computation.
So if you divide the color value by 65536, and take the floor of the result, you'll find back the r value (between 0 and 255).
That is, r = floor(color_value / 65536)
Now that you know the r value, you can remove its influence in the color value, by computing the remainder_gb.
remainder_gb = color_value - 65536 * r
Now you'll also remark in your post, that the green quantity is multiplied by 256 in the color value's computation.
So if you divide the remainder by 256, and again take the floor of the result, you'll find back the g value (also between 0 and 255).
That is, g = floor(remainder_gb / 256)
Good, you now also know the g value. You can remove its influence in the color value, by computing the remainder_b.
remainder_b = remainder_gb - 256 * g
Well, remainder_b… is the blue (b) value (again between 0 and 255)

Notice: A Scratch! color may also have a certain transparency.
To neglect this aspect and just find r, g and b accurately, you should first do this:
color_value = color_value modulo 16777216.
Greetz,
Dimitri
Last edited by dimitrip (Jan. 12, 2021 22:55:16)
- Discussion Forums
- » Help with Scripts
-
» Converting Colors to RGB