Various incomplete versions of this question have been asked a few times and all the answers have been lacking. With Android and Camera2 in order to set a manual white balance you have to define two things; color correction gains (which apply to the Bayer channels) and a color correction transform matrix (which applies to the demosaic RGB). On the iOS platform there is an API to magically get the color correction gains to account for a given color temperature (Kelvin) and tint.
One option for Android is to just use the built-in AWB mode presets. For example if I want to account for a yellow ~2850K light source I can just set CONTROL_AWB_MODE_INCANDESCENT. There are only 7 of these presets to flip between, so the resolution of what type of color temperatures you can account for is crude and you have no way to control the tint.
Basically every answer about Android Camera2 & custom white balance end up with some code based on this article (https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html) to calculate the red and blue color correction gains. This algorithm is actually the opposite of what I am looking for. For example setting incandescent ~2850K for the AWB mode will probably make the output look blue since its trying to account for a rather yellow light source (assuming you have a higher color temp of light in the room). If you use the algorithm in this article then 2850K will always color your image bright yellow, its acting like the incandescent source itself rather than accounting for a light source at that temperature.
None of the previous answers take the color correction transform into account either (they only set the gains), but the color correction transform must be set. When I read out the resulting gains and color correction transform when setting various AWB modes they are both changing.
So here are my questions:
What is an algorithm which can produce an equivalent result to the iOS API which converts a temp and tint into gain values. I want the gain values that account for a light source at the given temp (and not act as a light source) - so the gain values from 1,000K would likely turn your image blue (trying to account for a very orange light source) and 10,000K would turn things orange (trying to account for a very blue light source). The Filmic app for example does a great job of setting the temp and tint this way in their manual white balance controls.
What is an algorithm to convert back? If I read the current gain values and color correction transform from a CaptureResult when AWB is active - how do I get the temperature that the AWB algorithm is currently accounting for?
How do you know what to set the color correction transform to for the capture request!?? The only thing that seems somewhat promising is to either use SENSOR_FORWARD_MATRIX1 / SENSOR_FORWARD_MATRIX2? Or read the value from a previous capture result where AWB was running and then use that value? No one every brings this one up, but it seems absolutely necessary to set correctly and makes a huge difference.
What amounts to one easy API call on iOS apparently requires a PHD in Bayer filters, the Planckian locus, and color theory to derive the algorithms to do the same thing with Android/Camera2.