Visible light spectrum to color space I need to be able to convert an arbitrary emission spectrum in the visible spectrum range (i.e. for every wavelength between 380 and 780, I have a number between 0 and 1 that represents the "intensity" or dominance of that wavelength), and I need to be able to map any given spectrum into a particular color space (for now I need RGB or CIE-XYZ). Is it possible?
For the spectrum say I have the emission spectrum of a white light, then every wavelength in the spectrum will have an intensity of 1, whereas for a green-bluish light I'd have most of the wavelengths between 500 and 550 with an intensity close to 1, with other wavelengths gradually dropping in intensity. So the first spectrum should be converted to pure white whereas the other one would be converted to a green-bluish color in any color space.
Is there a way to do this?
 A: Human eye has three types of color receptors which respond differently to different parts of the spectrum. See this chart.
One way to tackle your challenge is to basically simulate what the eye does: you take the spectrum as input, calculate how much it would excite each of the three color receptors based on their sensitivity to different parts of the spectrum and then use the three resulting numbers as RGB corresponding to the spectrum.
In order to compute the excitation level, you can integrate the product of the sensitivity SC(λ) of each of the three color receptors with your spectral power distribution P(λ) to obtain the three RGB numbers:
\begin{equation}
R = \int_{0}^{+\infty} S_R(\lambda) P(\lambda) d\lambda
\end{equation}
\begin{equation}
G = \int_{0}^{+\infty} S_G(\lambda) P(\lambda) d\lambda
\end{equation}
\begin{equation}
B = \int_{0}^{+\infty} S_B(\lambda) P(\lambda) d\lambda
\end{equation}
For prototyping you can probably just assume the sensitivity SC(λ) functions to be appropriately scaled and translated Gaussian functions of the wavelength. As you refine your model you should seek better sensitivity functions for each of the three types of color receptors.
A: The other answer is correct, but doesn't give the full information on how to do the calculation.
First, what you need is so called color matching functions (CMF). There are multiple families of them. The most used is the CIE 1931 color matching functions that characterize human eye in the 2° central part of the field of view (which corresponds to the fovea centralis of the retina).
The CMFs $f_n(\lambda)$ should be used as weight functions for the spectral power distribution $p(\lambda)$: to yield $n^{\text{th}}$ tristimulus value $c_n$, do
$$c_n=\int_{\lambda_{\text{min}}}^{\lambda_{\text{max}}} f_n(\lambda)p(\lambda)\,d\lambda.$$
Tables of numeric values of various CMFs including the CIE 1931 ones can be found e.g. here.
These CMFs give you a translation from spectral power density to CIE XYZ color space. The next step is to convert these values to the target color space. For example, the conversion to sRGB takes the three values $X,$ $Y,$ $Z$ as a vector, applies a particular linear transformation to this vector, and then applies the sRGB transfer function to each component of the resulting vector to yield the final gamma-compressed nonlinear values.
The matrix of this linear transformation can be found in Wikipedia for sRGB, or at the Bruce Lindbloom's page that lists forward and backward matrices for more RGB color spaces.
The sRGB transfer function is also given on the Wikipedia page linked above.
