I'm wondering how to convert programmatically (i.e. with a programming language rather than an editor) from one colour temperature to another?

If, for example, I make the assumption (and it's a big assumption) that a white balance algorithm could take a processed image (e.g. JPEG, etc.) and place the overall temperature in a scene at 6500, how would you algorithmically "cool" or "warm" the scene to a specific temperature? 

Obviously this is a common operation in an image editing program, but these typically operate on RAW images which have no processing applied (colour space manipulation or otherwise). In the case of RAW files, perhaps the individual RAW files contain sufficient information (in the file header) regarding the colour calibration of the camera sensor to make the specific temperature transformation more deterministic?

By the way, I asked [this question on the regular Stack Overflow](https://stackoverflow.com/questions/6060087/adjust-white-balance) and it was suggested I may get a better answer here.