I agree that bithift is clearer and probably works better.
However, if you want to get a mathematical answer due to language support (for example, do it in a spreadsheet), modulus% and trunc () or floor () make this simple:
Assuming 8-bit values โโfor red, green, and blue, of course (0-255):
var rgbTotal = red * 65536 + green * 256 + blue; var R = Math.trunc( rgbTotal / 65536 ); var G = Math.trunc( ( rgbTotal % 65536 ) / 256 ); var B = rgbTotal % 256;
Discussion: Pointing to the answer itself, RGB values โโalmost always have a serial number with a direct order, of course, on web pages, jpeg and png. Personally, I think it's best to multiply red by 65536 instead of blue, unless you are working with a library that requires otherwise.
To return to individual values:
- For R, simply divide by 65536 and trim the remainder.
- For G, we drop R through mod 65536, then divide by 256, trimming the remainder (the remainder is blue).
- For B, we take mod 256, which gets rid of the two high bytes.
For R & G, the number must be truncated to discard the language-specific remainder, basically we need an integer. In javascript, Math.trunc () is an easy way, and Math.floor () also works. This is not necessary for B, since this is the remainder of the module - however, it also assumes that we do not need error checking, for example, from user input, i.e. the value for B is always an integer 0-255.
Myndex
source share