I am working with images of star fields. For a given star I want to obtain a representative RGB color in a given image working colorspace, which may not be sRGB. (If I only had to worry about sRGB the problem is already solved.) I can look up the details of a star in stellar catalogues and obtain the star's B-V (this is a photometric measure, the important thing about it is that there is a formula for calculating the effective temperature of a star from its B-V). I can convert the effective temperature into a set of CIE1931 xy coordinates which then get converted into XYZ (again, how to do this is not in question). I'm using lcms2 to perform the color space transformations.
To provide a reasonably natural representation of star color the Sun is taken as white. This is effectively a white point of D58, whereas the working colorspace of the image may be sRGB or Rec2020 (white point D65) or Adobe RGB (white point D50) or potentially others.
I'm not entirely sure how to get the colors looking correct.
If I convert the XYZ to the working ICC profile using the Relative Colorimetric intent the white point looks wrong, I think because it's making the white point of the working profile act as the white point of the data whereas I want them to have their own white point, different to that of the image I'm placing them in.
Instead, should I first be converting XYZ to an intermediate RGB ICC profile with a D58 white point (effectively using that as a source profile) and then converting that RGB value to the image working profile using an Absolute Colorimetric intent?
OK, I'm going to answer this myself as I got it working.
Step 1: look up the B and V magnitudes of each star from a stellar catalogue (e.g. Simbad). I'll skip over this because it's not central to the question and the catalogues have reasonably documented query APIs.
Step 2: Calculate B-V for the star:
Step 3: Convert B-V to temperature. The simplest way is using the formula implemented by the following function (reference in the comment):
You can try to look up the spectral type for each star and use separate bvToT calculations for each spectral type, but the above is generally good enough especially if you're dealing statistically with a large number of stars.
Step 4: Convert temperature to CIE xy: There are several approaches you can use here. The first is to use Mitchell Charity's Black body temperature to CIExy conversion tables. There are versions of these using the 1931 2 degree CMF with Judd-Vos corrections and the 1964 10 degree CMF and they are available for D65 and D58 white references. Implementation of a look-up table and interpolation is trivial. The tables claim validity from 1,000K to 40,000K. Secondly you can use a cubic spline method by Kim et al, as per the function below (reference in the comment):
The valid temperature range isn't as wide but it avoids storing a LUT. I'm starting to use the lcms2 API for datatypes now, because of what comes next. Finally, BQ Octantis proposed a formula on the Cloudy Nights astronomy forum. Reference provided in the comment:
This is a single 6th order spline, but requires expensive function calls to pow() to compute.
Step 5: Convert xyY to XYZ: This is trivial using lcms2:
where xyY is the black body color you calculated at step 4.
Step 6: Adapt the chromaticity: the Planckian locus formulas are based on D65 whereas the lcms2 built in XYZ profile is based on D50 so we do a chromatic adaptation:
Step 7: convert to working color space RGB: Finally, create a cmsHTRANSFORM from XYZ to your chosen working color space. Assume your working profile is cmsHPROFILE profile...
Alternatives: There are also straightforward lookup tables that convert directly from B-V values to sRGB R, G and B values. If all you care about is sRGB then they're absolutely fine, however while the method sketched out above is a little more complex it exposes all the workings and also allows you to generate R, G and B values (or even CMYK values, I guess) that are correct for whatever color space you are working in.