In the RGB color model, color values are represented in the 0-255
range. 0
is not a dummy value, so in the 0-255
range, there are 256 colors.
When using HSL (or something similar), I'd expect 50% to correspond to rgb(127,127,127)
because 50% of 256 colors is 128, and 128 corresponds to 127 in the 0-indexed 0-255
range. However, that's not the case. 50% gets converted to 128. Why is that?
I came across (only) this post Why the 50% gray's RGB value is 128 instead of 127 in Photoshop?, but the answers are talking about what/how, and not why.
The straightforward method to calculate 50% would be simply to take the half of 255 (max value) and round it (to get an integer), and that seems to be what the common converters doing, but isn't this wrong?
A better approach would be to calculate the range size, take its 50%, and then get the corresponding value in the 0-indexed range
( ((255-0) + 1) * 0.5 ) - 1 + 0 = 127
( ((rangeMax-rangeMin) + 1) * percentage ) - 1 + rangeMin = 127
Not doing this would imply that, from the computer's perspective, 0 is ignored and there are 255 colors, not 256. So 50% of 255 is 127.5, and it gets rounded to 128. Is that the case? If so, that means the percentage values are not exact.
Here's an image to make the case easier to follow:
Okay, I noticed this image isn't really explanatory after HolyBlackCat's comments. The 50% is direction dependent. If we're going from 0 to 255, 50% corresponds to 127, but from 255 to 0, it's 128. This is a problem (when the range size is even). Maybe the rouding (up) is there to fix this and make the calculation direction independent. I suppose it's arbitrary at this point whether to round up or down.