[Bf-blender-cvs] [37fecd8ce31] temp-explicit-colors: Blenlib: Explicit Colors.

Jeroen Bakker noreply at git.blender.org
Wed Apr 14 16:11:02 CEST 2021


Commit: 37fecd8ce31595ccf75658ffecbfbb1c09b45bbf
Author: Jeroen Bakker
Date:   Wed Apr 14 16:09:18 2021 +0200
Branches: temp-explicit-colors
https://developer.blender.org/rB37fecd8ce31595ccf75658ffecbfbb1c09b45bbf

Blenlib: Explicit Colors.

Colors needs to be explicitly typed.
Currently not fully working as constructors overriding selects incorrect
constructor.

===================================================================

M	source/blender/blenkernel/BKE_attribute_access.hh
M	source/blender/blenkernel/BKE_attribute_math.hh
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/attribute_math.cc
M	source/blender/blenkernel/intern/geometry_component_mesh.cc
M	source/blender/blenlib/BLI_color.hh
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/tests/BLI_color_test.cc
M	source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc
M	source/blender/functions/intern/cpp_types.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/nodes/node_geo_attribute_clamp.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_color_ramp.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_compare.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_mix.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_sample_texture.cc
M	source/blender/nodes/intern/node_geometry_exec.cc
M	source/blender/nodes/intern/node_socket.cc
M	source/blender/nodes/intern/node_tree_multi_function.cc
M	source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc
M	source/blender/nodes/shader/nodes/node_shader_valToRgb.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_attribute_access.hh b/source/blender/blenkernel/BKE_attribute_access.hh
index 120b4e08b9c..df20171bd88 100644
--- a/source/blender/blenkernel/BKE_attribute_access.hh
+++ b/source/blender/blenkernel/BKE_attribute_access.hh
@@ -307,12 +307,12 @@ using FloatReadAttribute = TypedReadAttribute<float>;
 using Float2ReadAttribute = TypedReadAttribute<float2>;
 using Float3ReadAttribute = TypedReadAttribute<float3>;
 using Int32ReadAttribute = TypedReadAttribute<int>;
-using Color4fReadAttribute = TypedReadAttribute<Color4f>;
+using ColorGeometryReadAttribute = TypedReadAttribute<ColorGeometry>;
 using BooleanWriteAttribute = TypedWriteAttribute<bool>;
 using FloatWriteAttribute = TypedWriteAttribute<float>;
 using Float2WriteAttribute = TypedWriteAttribute<float2>;
 using Float3WriteAttribute = TypedWriteAttribute<float3>;
 using Int32WriteAttribute = TypedWriteAttribute<int>;
-using Color4fWriteAttribute = TypedWriteAttribute<Color4f>;
+using ColorGeometryWriteAttribute = TypedWriteAttribute<ColorGeometry>;
 
 }  // namespace blender::bke
diff --git a/source/blender/blenkernel/BKE_attribute_math.hh b/source/blender/blenkernel/BKE_attribute_math.hh
index 16fc0db60fb..f6a93a7dbd2 100644
--- a/source/blender/blenkernel/BKE_attribute_math.hh
+++ b/source/blender/blenkernel/BKE_attribute_math.hh
@@ -46,7 +46,7 @@ void convert_to_static_type(const CustomDataType data_type, const Func &func)
       func(bool());
       break;
     case CD_PROP_COLOR:
-      func(Color4f());
+      func(ColorGeometry());
       break;
     default:
       BLI_assert_unreachable();
@@ -91,9 +91,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 ColorGeometry mix3(const float3 &weights,
+                          const ColorGeometry &v0,
+                          const ColorGeometry &v1,
+                          const ColorGeometry &v2)
 {
-  Color4f result;
+  ColorGeometry result;
   interp_v4_v4v4v4(result, v0, v1, v2, weights);
   return result;
 }
@@ -198,15 +201,16 @@ class SimpleMixerWithAccumulationType {
   }
 };
 
-class Color4fMixer {
+class ColorGeometryMixer {
  private:
-  MutableSpan<Color4f> buffer_;
-  Color4f default_color_;
+  MutableSpan<ColorGeometry> buffer_;
+  ColorGeometry 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<ColorGeometry> buffer,
+                     ColorGeometry default_color = {0, 0, 0, 1});
+  void mix_in(const int64_t index, const ColorGeometry &color, const float weight = 1.0f);
   void finalize();
 };
 
@@ -223,10 +227,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<ColorGeometry> {
+  /* Use a special mixer for colors. ColorGeometry 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)
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 5bd3b990a63..af87067f6d6 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -244,7 +244,7 @@ const blender::fn::CPPType *custom_data_type_to_cpp_type(const CustomDataType ty
     case CD_PROP_INT32:
       return &CPPType::get<int>();
     case CD_PROP_COLOR:
-      return &CPPType::get<Color4f>();
+      return &CPPType::get<ColorGeometry>();
     case CD_PROP_BOOL:
       return &CPPType::get<bool>();
     default:
@@ -267,7 +267,7 @@ CustomDataType cpp_type_to_custom_data_type(const blender::fn::CPPType &type)
   if (type.is<int>()) {
     return CD_PROP_INT32;
   }
-  if (type.is<Color4f>()) {
+  if (type.is<ColorGeometry>()) {
     return CD_PROP_COLOR;
   }
   if (type.is<bool>()) {
@@ -484,7 +484,7 @@ ReadAttributePtr CustomDataAttributeProvider::try_get_for_read(
       case CD_PROP_INT32:
         return this->layer_to_read_attribute<int>(layer, domain_size);
       case CD_PROP_COLOR:
-        return this->layer_to_read_attribute<Color4f>(layer, domain_size);
+        return this->layer_to_read_attribute<ColorGeometry>(layer, domain_size);
       case CD_PROP_BOOL:
         return this->layer_to_read_attribute<bool>(layer, domain_size);
       default:
@@ -518,7 +518,7 @@ WriteAttributePtr CustomDataAttributeProvider::try_get_for_write(
       case CD_PROP_INT32:
         return this->layer_to_write_attribute<int>(layer, domain_size);
       case CD_PROP_COLOR:
-        return this->layer_to_write_attribute<Color4f>(layer, domain_size);
+        return this->layer_to_write_attribute<ColorGeometry>(layer, domain_size);
       case CD_PROP_BOOL:
         return this->layer_to_write_attribute<bool>(layer, domain_size);
       default:
diff --git a/source/blender/blenkernel/intern/attribute_math.cc b/source/blender/blenkernel/intern/attribute_math.cc
index 4ff3a6ceff5..54fdcdf93a1 100644
--- a/source/blender/blenkernel/intern/attribute_math.cc
+++ b/source/blender/blenkernel/intern/attribute_math.cc
@@ -18,18 +18,21 @@
 
 namespace blender::attribute_math {
 
-Color4fMixer::Color4fMixer(MutableSpan<Color4f> output_buffer, Color4f default_color)
+ColorGeometryMixer::ColorGeometryMixer(MutableSpan<ColorGeometry> output_buffer,
+                                       ColorGeometry default_color)
     : buffer_(output_buffer),
       default_color_(default_color),
       total_weights_(output_buffer.size(), 0.0f)
 {
-  buffer_.fill(Color4f(0, 0, 0, 0));
+  buffer_.fill(ColorGeometry(0, 0, 0, 0));
 }
 
-void Color4fMixer::mix_in(const int64_t index, const Color4f &color, const float weight)
+void ColorGeometryMixer::mix_in(const int64_t index,
+                                const ColorGeometry &color,
+                                const float weight)
 {
   BLI_assert(weight >= 0.0f);
-  Color4f &output_color = buffer_[index];
+  ColorGeometry &output_color = buffer_[index];
   output_color.r += color.r * weight;
   output_color.g += color.g * weight;
   output_color.b += color.b * weight;
@@ -37,11 +40,11 @@ void Color4fMixer::mix_in(const int64_t index, const Color4f &color, const float
   total_weights_[index] += weight;
 }
 
-void Color4fMixer::finalize()
+void ColorGeometryMixer::finalize()
 {
   for (const int64_t i : buffer_.index_range()) {
     const float weight = total_weights_[i];
-    Color4f &output_color = buffer_[i];
+    ColorGeometry &output_color = buffer_[i];
     if (weight > 0.0f) {
       const float weight_inv = 1.0f / weight;
       output_color.r *= weight_inv;
diff --git a/source/blender/blenkernel/intern/geometry_component_mesh.cc b/source/blender/blenkernel/intern/geometry_component_mesh.cc
index 150cc4589c8..0a55f68dc89 100644
--- a/source/blender/blenkernel/intern/geometry_component_mesh.cc
+++ b/source/blender/blenkernel/intern/geometry_component_mesh.cc
@@ -812,16 +812,16 @@ static void set_loop_uv(MLoopUV &uv, const float2 &co)
   copy_v2_v2(uv.uv, co);
 }
 
-static Color4f get_loop_color(const MLoopCol &col)
+static ColorGeometry get_loop_color(const MLoopCol &col)
 {
-  Color4f srgb_color;
+  ColorGeometry srgb_color;
   rgba_uchar_to_float(srgb_color, &col.r);
-  Color4f linear_color;
+  ColorGeometry linear_color;
   srgb_to_linearrgb_v4(linear_color, srgb_color);
   return linear_color;
 }
 
-static void set_loop_color(MLoopCol &col, const Color4f &linear_color)
+static void set_loop_color(MLoopCol &col, const ColorGeometry &linear_color)
 {
   linearrgb_to_srgb_uchar4(&col.r, linear_color);
 }
@@ -1175,9 +1175,9 @@ static ComponentAttributeProviders create_attribute_providers_for_mesh()
       CD_PROP_COLOR,
       CD_MLOOPCOL,
       corner_access,
-      make_derived_read_attribute<MLoopCol, Color4f, get_loop_color, ATTR_DOMAIN_CORNER>,
+      make_derived_read_attribute<MLoopCol, ColorGeometry, get_loop_color, ATTR_DOMAIN_CORNER>,
       make_derived_write_attribute<MLoopCol,
-                                   Color4f,
+                                   ColorGeometry,
                                    get_loop_color,
                                    set_loop_color,
                                    ATTR_DOMAIN_CORNER>);
diff --git a/source/blender/blenlib/BLI_color.hh b/source/blender/blenlib/BLI_color.hh
index e57a5109a66..de87a837e38 100644
--- a/source/blender/blenlib/BLI_color.hh
+++ b/source/blender/blenlib/BLI_color.hh
@@ -22,7 +22,48 @@
 
 namespace blender {
 
-struct Color4f {
+/* Spaces are defined as classes to be extended with meta-data in the future.
+ * The meta data could contain CIE mapping and whitepoints. */
+class Srgb {
+};
+class LinearRGB {
+};
+
+/* Enumeration containing the different alpha modes. */
+enum class eAlpha : uint8_t {
+  /* Alpha is unassociated (color is straight). */
+  Straight,
+  /* Alpha is associated (color is premultiplied with alpha). */
+  Premultiplied,
+};
+
+template<typename Space, eAlpha Alpha> struct Color4f;
+template<typename Space, eAlpha Alpha> struct Color4b;
+
+/* Internal roles. To shorten the type names and hide complexity in areas where transformations are
+ * unlikely to happen. */
+using ColorRender = Color4f<LinearRGB, eAlpha::Premultiplied>;
+using ColorReference = Color4f<LinearRGB, eAlpha::Premultiplied>;
+using ColorCompositor = Color4f<LinearRGB, eAlpha::Premultiplied>;
+using ColorTheme = Color4b<Srgb, eAlpha::Straight>;
+using ColorGeometry = Color4f<LinearRGB, eAlpha::Premultiplied>;
+
+MINLINE void associate_alpha(const Color4f<LinearRGB, eAlpha::Straight> &src,
+                             Color4f<LinearRGB, eAlpha::Premultipl

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list