For beginning developers colors are often thought of as being 4 values that make up any color. But that is of course to limited.
In C we didn’t spend time to annotate what we meant when using colors. Now with
CPP and https://developer.blender.org/diffusion/B/browse/master/source/blender/blenlib/BLI_color.hh
we can make colors more explicit (fe which colorspace/role and alpha association is used).
Internally in Blender due to legacy a lot is bound to the sRGB color space and Rec709.
OpenColorIO uses roles to add more flexibility to storage and handling of colors.
Blender has defined several roles that maps to a color space that is in config.ocio
or
can be changed by the user via the color management panel, but many areas in blender still uses the legacy pipeline.
My proposal would that any usage of BLI_color.hh
would need to annotate its Colorspace or Role
and its alpha association.
There are be multiple ways how to do this. For example as a template:
Color4b<Space::sRGB, Alpha::Unassociated>
, Color4f<Space::SceneReference, Alpha::Associated>
.
This could be used to clarify the way how colors are used and we could
start hooking up (OCIO) transformation between them.
Another proposal is to use subclasses. Modern compilers are
able to remove overhead when addressing to final classes only. Benefit is that the compiler
give more understandable errors. Con is readability, With the templating internal parts are
visible and show to developers that it is something to be thinking of.
For the conversion we could use function or constructor overloading to ensure compilers can make
the right decision.
ColorSrgb src_color;
ColorSceneReference dst_color;
convert_color(&src_color, &dst_color);
/* NOTE: only allow final classes as parameter. */
void convert_color(const ColorSrgb &in_color, ColorSceneReference &r_color)
{
}
ColorSceneReference::ColorSceneReference(const ColorSrgb &in_color)
{
}
Example enum items. Space enum contains both concrete spaces limited to what we use internally and role based spaces that can be configured with OCIO.
enum class Alpha {
/* Color is premultiplied with alpha. */
Associated,
/* Color isn't premultiplied with alpha. */
Unassociated,
};
/* The roles/spaces should be limited to what blender uses internally. */
enum class Space {
/* Internal used concrete spaces. */
Srgb,
Rec709,
XYZ,
...
/* Roles. */
Rendering,
Compositing,
SceneReference,
ColorPicking,
TexturePainting,
Timing,
DefaultByte,
DefaultFloat,
...
};