
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
62 lines
2.0 KiB
C++
62 lines
2.0 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include "BKE_attribute_math.hh"
|
|
|
|
namespace blender::attribute_math {
|
|
|
|
ColorGeometryMixer::ColorGeometryMixer(MutableSpan<ColorGeometry4f> output_buffer,
|
|
ColorGeometry4f default_color)
|
|
: buffer_(output_buffer),
|
|
default_color_(default_color),
|
|
total_weights_(output_buffer.size(), 0.0f)
|
|
{
|
|
buffer_.fill(ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
|
|
}
|
|
|
|
void ColorGeometryMixer::mix_in(const int64_t index,
|
|
const ColorGeometry4f &color,
|
|
const float weight)
|
|
{
|
|
BLI_assert(weight >= 0.0f);
|
|
ColorGeometry4f &output_color = buffer_[index];
|
|
output_color.r += color.r * weight;
|
|
output_color.g += color.g * weight;
|
|
output_color.b += color.b * weight;
|
|
output_color.a += color.a * weight;
|
|
total_weights_[index] += weight;
|
|
}
|
|
|
|
void ColorGeometryMixer::finalize()
|
|
{
|
|
for (const int64_t i : buffer_.index_range()) {
|
|
const float weight = total_weights_[i];
|
|
ColorGeometry4f &output_color = buffer_[i];
|
|
if (weight > 0.0f) {
|
|
const float weight_inv = 1.0f / weight;
|
|
output_color.r *= weight_inv;
|
|
output_color.g *= weight_inv;
|
|
output_color.b *= weight_inv;
|
|
output_color.a *= weight_inv;
|
|
}
|
|
else {
|
|
output_color = default_color_;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace blender::attribute_math
|