I am writing a UI for a legacy computer called an Amiga (some of you may remember these). They had display modes that had fixed numbers of colours - 2,4,8,16 etc. and you could set any of these using the RGB of 0 to 255.
So on a 4 colour display you could have:
The maximum number of colours you could have on 'normal' display was 255 (there were some special display modes that pushed this higher but I don't need to worry about these).
I am trying to write some C code that will read through the display's colour list and find the reddest, greenest, and bluest colour, but I just cannot get my head around the comparisons involved.
If I have 3 vars -red, green & blue - all ints which hold the current colours rgb vals and 3 more - stored_red, stored_stored_green & stored_blue.
How can I write a function that will remember the colour number which has the most red in relation to the other colours?
I've tried:
If ((red>stored_red) && ((blue <stored_blue) || (green<stored_green)))
But this doesn't work. I think I need to work on ratios but just cannot figure out the maths.
There are different ways to check for maximum "redness"/"greenness" etc, this first simple way here is what my answer originally used:
/* for all palette (color table) entries */
if(red > stored_red) {
max_red_index = current_index;
stored_red = red;
}
if(blue > stored_blue) {
max_blue_index = current_index;
stored_blue = blue;
}
and the same for green of course.
This will just give the colors with the maximum components, but after the comment of @Chris Turner I think this is probably not what you want.
Another way could be to check the ratio of red to the other colors (I'll just use only red from now on):
redness = red*2/(blue + green + 1)
This gives a number between 1 and 510 for "redness", it does not consider brightness. For example R-G-B 50-10-10 is more red (4) than 255-100-100 (2).
Many other formulas are possible, something like
redness = red*red/(blue + green + 1)
would also take the brightness into account and would consider 255-100-100 more red than 50-10-10.
To use these formulas, just set the red
variable from above to the result of the redness formula.