RGB is an additive color model that describes a color as amounts of red, green and blue light, written on the web as three channel values from 0 to 255. RGB stands for Red, Green, Blue. Screens mix those three lights, so rgb(0 0 0) is black and rgb(255 255 255) is white. In CSS the rgb() function is defined in CSS Color Module Level 4, and its values are interpreted in the sRGB color space.
Each channel is stored in one byte, so it holds an integer from 0 to 255. Three bytes make a 24-bit color. A hex color such as #3b82f6 is the same three bytes written in base 16: 3b is 59, 82 is 130 and f6 is 246.
CSS accepts two notations for the channels:
rgb(59 130 246) uses 0 to 255 per channel.rgb(23.1% 51.0% 96.5%) maps 0% to 0 and 100% to 255. CSS defines the percentage range this way because many graphics engines stored channels as a single byte.rgb(59 130 246 / 50%). The older comma form rgba(59, 130, 246, 0.5) still works.Values outside the range are not an error. CSS clamps them when the value is parsed, so rgb(300 0 0) becomes rgb(255 0 0).
const hex = "#3b82f6";
const [r, g, b] = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16));
console.log(`rgb(${r} ${g} ${b})`);
console.log(`rgb(${(r / 255 * 100).toFixed(1)}% ${(g / 255 * 100).toFixed(1)}% ${(b / 255 * 100).toFixed(1)}%)`);
console.log(256 ** 3);
const lin = v => ((v / 255 + 0.055) / 1.055) ** 2.4;
console.log((lin(128)).toFixed(4));
console.log((128 / 255).toFixed(4));
rgb(59 130 246)
rgb(23.1% 51.0% 96.5%)
16777216
0.2159
0.5020
The last two lines show why channel values are not light amounts: 128 is 50.2% of the number range but only about 21.6% of the light.
RGB with 8 bits per channel has 16,777,216 colors, because 256 x 256 x 256 = 16,777,216. CSS rgb() numbers always map to that 0 to 255 reference range.
Each RGB channel ranges from 0 to 255 in CSS. 0 means none of that light and 255 means the maximum the display can show for that channel.
rgb(59, 130, 246 / 50%) is invalid. Commas go with the legacy form, the slash with the modern form.rgb(0.2 0.5 0.9) means nearly black, because plain numbers are on the 0 to 255 scale. Use percentages or multiply by 255.