What your looking to do can be a very complex problem space. Color theory, color correction/white balance, etc. is a very complex field. To make sure I understand, you are trying to create an algorithm that can properly set the white balance of a scene based on the illuminating light source in the scene. It might be possible to do what you need to do, and many of the algorithms you would need to use should be available these days.
Some points to consider, and factor into your processing:
- No in-scene light source
- For example: The sun illuminating a scene from behind the photographer
- Another example: Off-scene flash or other lighting
- This is probably the biggest issue, and would make it pretty much impossible to do any light-source based color correction
- Multiple light sources
- It is possible for there to be multiple light sources with the same color temperature
- Of more importance is multiple light sources with differing color temperature
- Key Question: What do you want to happen if a scene is illuminated with multiple light sources of differing color?
- Ambient vs. point light sources
- Example: The sun illuminating a room through a window, with additional lighting illuminating the shaded parts
- Key Question: What source do you want to white balance on?
- Light sources of radical color
- Sometimes you have scenes illuminated by primary light sources of radical colors
- For example: red, green, blue, purple, yellow, etc.
- The Blue/Yellow/Red plane can be considered extremes of normal color temperature
- The Green/Purple/Magenta plane could be considered the extremes of tint
- No saturated color, primary or secondary, makes a good target for color balancing a scene, as it would likely produce pronounced color cast in your image
- Key Question: What do you do when your scene has no common ambient or primary lighting, and is entirely or primarily lit by radical colors?
If we exclude no in-scene lighting and radical colors from the problem space, it is probably possible to identify and measure the color temperature of in-scene light sources. There are some fairly advanced shape detection algorithms available these days that, provided you have a decent base or known light source shapes, would make it possible to identify all the light sources in a scene. Once you have identified each light source, you could probably use some kind of tristimulus calculation to determine the color temperature of the light source, taking the green, red, and blue subpixel values or RAW bayer pixel values with proper weights to generate your three luminance peaks. An average of samples from the area of the light source (as discovered by your earlier identification step) could produce a fairly accurate color temperature reading (but probably not entirely accurate, as digital data will always be less accurate than original analog data).
Your algorithm becomes particularly complex when you involve non-standard or radical scene illumination. Extremely saturated light sources won't make good white balance targets, since they are far from the "white" range of a color gamut. It is very important to know what part of your scene is most "neutral" so that you can pick a proper "gray or white" to base your white balance on, as that is fundamentally how white balance works. It may be possible to color balance your scene based on a different base color, such as red, however I don't know exactly how you would need to modify existing mathematical formulas to accommodate such an approach (there is a fair amount of complex math involved in color theory.)
The most difficult situation is when there are no in-scene light sources. If the scene is illuminated by an off-scene light source, it would be pretty much impossible to calculate its color temperature. If you wanted to take a fairly radical approach to camera design, you might put some basic light metering sensors on all sides of the camera body. That would allow light to be measured from all directions, however it would not be entirely accurate, and the gains for all the extra complexity would likely be minor. Another approach might be to create a hotshoe device that could connect to multiple spot meters that could provide accurate illumination readings to the camera for any light source you point a meter at. Scenes with multiple light sources or sources with radical color would still make for a very complex algorithm.
Trying to determine the color temperature of your scenes light sources from a simple photo of a color correction card, for example, would be extremely difficult if not flat out impossible. You would have to make too many assumptions about, or you would have to know ahead of time, what kind of light was illuminating your scene. This is generally why white balance/color corrections are usually done by setting the scenes white balance based on a known neutral color in a scene. Photographing a landscape twice in the same lighting, once with and once without a color correction card, allows you to pick a valid neutral or pure white color during post processing, which will set proper color balance. I myself have the X-Rite ColorChecker Passport, which includes two rows of color correction swatches. One row is for landscapes and the other for portraits, and they progress from slightly cool to slightly warm color patches. I can set the kind of color balance that I want for my scenes, which tends to be warmer for landscape photography, and more neutral white toned (5500k-6500k) for things like wildlife or macro.
If you are doing something considerably more specialized, you may be able to define a smaller problem space within which you can create an algorithm that does what you need. The problem space would have to be fairly narrow, with rather well-defined parameters and limits, in order to generate enough well-known data to help the algorithm make the correct decisions when white balancing a scene. Any scene without primary light sources within the frame is going to be very difficult to do any automatic white balance or color correction.