Is it possible to calculate the color frequency of a pixel?
I mean, I get a pixel that is red but where this red sits on the spectrum in hertz?
Is this even possible to do even in a limited way?
thanks.
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It only takes a minute to sign up.
Sign up to join this communityYes, but the answer is a little more complicated than you were probably hoping for.
In short: one color in general corresponds to a distribution of different frequencies, or wavelengths.
You have three types of cone cells in your eyes [+ one type of rod cell]. Color-blind people have less than three types (or their response curves are closer together). It is believed a few people may exist, "tetrachromats", who have four types. Some species of octopi see in 11 dimensions of color.
Each type of cone cell is sensitive to certain wavelengths. If you have two lasers, you can draw a vertical line from the frequency of the laser up to each spectral response curve and add the values for each cone type to get the total "stimulus" per type. It is clear then that you can have a vast combination of frequency pairs that cause the same stimulus in each of the S, M, and L type cells! This is why a computer monitor can generate the color yellow using red and green subpixels. This same yellow can occur with photons of one wavelength that give the same total stimulus.
So the phenomenon of color isn't as simple as matching each one up to a wavelength. In fact, there are colors that humans can see that correspond to no single wavelength! No laser can produce the colors pink and purple.
Below is a chromaticity diagram. I won't go into too much detail about how this is constructed, but it roughly corresponds to all the types of colors you can see. You can also vary the luminance of the diagram (roughly corresponds to the illy-defined term "brightness").
Anyway, this diagram has a few interesting properties.
Now, with this background, I can answer your question a little more directly. Since monitors use three light sources per pixel, the nature of each subpixel's light must be characterized with its own spectral power distribution. For most purposes this can be approximated as a single wavelength. You then have to determine the three coefficients that weight each subpixel such that the sum of their values times the corresponding intensities on the stimulus response curve equals the same as which produces whatever color it is you're wondering about. Most color primaries data is freely available online, but I'll leave the math problem for someone else.
Each pixel on a typical computer monitor is actually made up of 3 subpixels that are of a single, fixed frequency. The control circuitry of the monitor adjusts the brightness of these three subpixels in order to feed light to your eye that your brain interprets as lying somewhere on the visible light spectrum.
The answers to nearly the same questions are available here.
What about just taking frequency ranges given on the Wikipedia article on color and pairing them with the hex codes for the background colors they subimpose? This would be a stepped function, but you could always make it piecewise linear, or approximate it with a bezier or some sort of basis function expansion.
Also, this would certainly have trouble being represented evenly across displays. Perhaps someone who understands color gamuts better could explain exactly what we mean by FF0000
? The peaks in the plot of responses of different cone cells given by Nick are at 420–440 nm for S, 530–540 nmf for M, and 560–580 nm for L (tristimulus values), which I think sRGB then somehow links to red, green, and blue values through a 3x3 matrix of some magic numbers.
You might be able to use those peak center locations to bang out a different first approximation than the one from the Wikipedia list of color frequencies. Then do a weighted average of the two methods! This is real science.
Edit: Ok, it looks like I ignored the real problem--we need $f:\mathbb{R}^3 \rightarrow\mathbb{R}^1$ to take us from RGB to frequency space! And apparently taking a linear combination of the three primaries won't cut it.
Another problem is that natural light isn't just a single frequency--it's a spectrum (which you could liken to a probability distribution $P(\lambda)$). So, I lied with that earlier function description. Natural light isn't really $\mathbb{R}^1$--it's actually more like $\mathbb{R}^{60}$, for a 5 nm discretization of a 300 nm range. This is converted to $X$, $Y$, and $Z$ by convolving this discretized spectrum of natural light with the three CIE color matching spectra $Q_i(\lambda)$(see equation 1) (that is, taking the inner product).
Then you can convert the $X$, $Y$, and $Z$ to $x$, $y$, and $z$ by normalizing them by their sum (equation 2), and then then convert them to RGB using those sRGB magic numbers (equation 3). Further shenanigans with a white point are required to represent (approximate?) out-of-gamut colors.
This is great for converting a known spectrum to a particular RGB triad, but I'm not sure how to go the other way. I guess you'd want to create a normal distribution $P(\lambda)$ such that the process I just described gets you the desired RGB triad, and you would find this $P(\lambda)$ through some sort of optimization problem? Then the "single frequency" would be the mean of this distribution? This is quickly getting to be very computationally expensive.