diff options
author | Jeroen Bakker <jbakker> | 2021-05-25 18:00:14 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-05-25 18:01:26 +0300 |
commit | fd94e033446c72fb92048a9864c1d539fccde59a (patch) | |
tree | 866890d08dca1a1a2daa18335a26abaf870292e8 /source/blender/blenkernel/BKE_attribute_math.hh | |
parent | b046bc536bec914013c678b552ce6cef7dd308e6 (diff) |
Blenlib: Explicit Colors.
Colors are often thought of as being 4 values that make up that can make any color.
But that is of course too limited. In C we didn’t spend time to annotate what we meant
when using colors.
Recently `BLI_color.hh` was made to facilitate color structures in CPP. CPP has possibilities to
enforce annotating structures during compilation and can adds conversions between them using
function overloading and explicit constructors.
The storage structs can hold 4 channels (r, g, b and a).
Usage:
Convert a theme byte color to a linearrgb premultiplied.
```
ColorTheme4b theme_color;
ColorSceneLinear4f<eAlpha::Premultiplied> linearrgb_color =
BLI_color_convert_to_scene_linear(theme_color).premultiply_alpha();
```
The API is structured to make most use of inlining. Most notable are space
conversions done via `BLI_color_convert_to*` functions.
- Conversions between spaces (theme <=> scene linear) should always be done by
invoking the `BLI_color_convert_to*` methods.
- Encoding colors (compressing to store colors inside a less precision storage)
should be done by invoking the `encode` and `decode` methods.
- Changing alpha association should be done by invoking `premultiply_alpha` or
`unpremultiply_alpha` methods.
# Encoding.
Color encoding is used to store colors with less precision as in using `uint8_t` in
stead of `float`. This encoding is supported for `eSpace::SceneLinear`.
To make this clear to the developer the `eSpace::SceneLinearByteEncoded`
space is added.
# Precision
Colors can be stored using `uint8_t` or `float` colors. The conversion
between the two precisions are available as methods. (`to_4b` and
`to_4f`).
# Alpha conversion
Alpha conversion is only supported in SceneLinear space.
Extending:
- This file can be extended with `ColorHex/Hsl/Hsv` for different representations
of rgb based colors. `ColorHsl4f<eSpace::SceneLinear, eAlpha::Premultiplied>`
- Add non RGB spaces/storages ColorXyz.
Reviewed By: JacquesLucke, brecht
Differential Revision: https://developer.blender.org/D10978
Diffstat (limited to 'source/blender/blenkernel/BKE_attribute_math.hh')
-rw-r--r-- | source/blender/blenkernel/BKE_attribute_math.hh | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/source/blender/blenkernel/BKE_attribute_math.hh b/source/blender/blenkernel/BKE_attribute_math.hh index 0afdc436415..ba683362e69 100644 --- a/source/blender/blenkernel/BKE_attribute_math.hh +++ b/source/blender/blenkernel/BKE_attribute_math.hh @@ -52,7 +52,7 @@ inline void convert_to_static_type(const CustomDataType data_type, const Func &f func(bool()); break; case CD_PROP_COLOR: - func(Color4f()); + func(ColorGeometry4f()); break; default: BLI_assert_unreachable(); @@ -78,8 +78,8 @@ inline void convert_to_static_type(const fn::CPPType &cpp_type, const Func &func else if (cpp_type.is<bool>()) { func(bool()); } - else if (cpp_type.is<Color4f>()) { - func(Color4f()); + else if (cpp_type.is<ColorGeometry4f>()) { + func(ColorGeometry4f()); } else { BLI_assert_unreachable(); @@ -123,9 +123,12 @@ inline float3 mix3(const float3 &weights, const float3 &v0, const float3 &v1, co } template<> -inline Color4f mix3(const float3 &weights, const Color4f &v0, const Color4f &v1, const Color4f &v2) +inline ColorGeometry4f mix3(const float3 &weights, + const ColorGeometry4f &v0, + const ColorGeometry4f &v1, + const ColorGeometry4f &v2) { - Color4f result; + ColorGeometry4f result; interp_v4_v4v4v4(result, v0, v1, v2, weights); return result; } @@ -165,9 +168,10 @@ template<> inline float3 mix2(const float factor, const float3 &a, const float3 return float3::interpolate(a, b, factor); } -template<> inline Color4f mix2(const float factor, const Color4f &a, const Color4f &b) +template<> +inline ColorGeometry4f mix2(const float factor, const ColorGeometry4f &a, const ColorGeometry4f &b) { - Color4f result; + ColorGeometry4f result; interp_v4_v4v4(result, a, b, factor); return result; } @@ -274,15 +278,16 @@ class SimpleMixerWithAccumulationType { } }; -class Color4fMixer { +class ColorGeometryMixer { private: - MutableSpan<Color4f> buffer_; - Color4f default_color_; + MutableSpan<ColorGeometry4f> buffer_; + ColorGeometry4f default_color_; Array<float> total_weights_; public: - Color4fMixer(MutableSpan<Color4f> buffer, Color4f default_color = {0, 0, 0, 1}); - void mix_in(const int64_t index, const Color4f &color, const float weight = 1.0f); + ColorGeometryMixer(MutableSpan<ColorGeometry4f> buffer, + ColorGeometry4f default_color = ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f)); + void mix_in(const int64_t index, const ColorGeometry4f &color, const float weight = 1.0f); void finalize(); }; @@ -299,10 +304,10 @@ template<> struct DefaultMixerStruct<float2> { template<> struct DefaultMixerStruct<float3> { using type = SimpleMixer<float3>; }; -template<> struct DefaultMixerStruct<Color4f> { - /* Use a special mixer for colors. Color4f can't be added/multiplied, because this is not +template<> struct DefaultMixerStruct<ColorGeometry4f> { + /* Use a special mixer for colors. ColorGeometry4f can't be added/multiplied, because this is not * something one should usually do with colors. */ - using type = Color4fMixer; + using type = ColorGeometryMixer; }; template<> struct DefaultMixerStruct<int> { static int double_to_int(const double &value) |