Do decimal values affect color channels in CSS?

34 Views Asked by At

I'm working on a data visualization library in JavaScript which depends on a "Color" class containing values for red, green, and blue channels from 0-255. Is there any reason to store decimal values for these color channels or is it safe to truncate to an integer?

E.g. in css, is

rgba(250, 0, 0, 100%)

the same as

rgba(250.1, 0, 0, 100%)

I know that hex color codes 00 through FF are equivalent to integer values 0 through 255, without decimals.

Humans actually being able to percieve 2 different colors is a different story here. I want to know:

  • Are colors are actually interpreted differently by CSS?
  • Does it depend on the user's monitor, browser, or GPU?
1

There are 1 best solutions below

0
Frederik Spang On

How the browsers choose to adjust the colours may depend, however according to the standard from W3C, it can be a decimal, following the CSS grammer

rgba()               = [ <legacy-rgba-syntax> | <modern-rgba-syntax> ]

<legacy-rgb-syntax>  = rgb( <percentage>#{3} , <alpha-value>? ) |
                       rgb( <number>#{3} , <alpha-value>? )
<legacy-rgba-syntax> = rgba( <percentage>#{3} , <alpha-value>? ) |
                       rgba( <number>#{3} , <alpha-value>? )

<modern-rgb-syntax>  = rgb( 
                         [ <number> | <percentage> | none]{3} 
                         [ / [<alpha-value> | none] ]?  )
<modern-rgba-syntax> = rgba( 
                         [ <number> | <percentage> | none]{3} 
                         [ / [<alpha-value> | none] ]?  )

And following the definition of <number>:

Number values are denoted by <number>, and represent real numbers, possibly with a fractional component.
When written literally, a number is either an integer, or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits; [...].