MeshLib C++ Docs
Loading...
Searching...
No Matches
MRColor.h
Go to the documentation of this file.
1#pragma once
2#include "MRMeshFwd.h"
3#include "MRVector3.h"
4#include "MRVector4.h"
5
6namespace MR
7{
10
11struct Color
12{
13 uint8_t r, g, b, a;
14
15 constexpr Color() noexcept : r{ 0 }, g{ 0 }, b{ 0 }, a{ 255 } {}
16 explicit Color( NoInit ) noexcept {}
17 MR_BIND_OVERLOAD_SUFFIX() constexpr Color( int r, int g, int b, int a ) noexcept : r{uint8_t(r)}, g{uint8_t(g)}, b{uint8_t(b)}, a{uint8_t(a)} {}
18 MR_BIND_OVERLOAD_SUFFIX(rgb) constexpr Color( int r, int g, int b ) noexcept : r{uint8_t(r)}, g{uint8_t(g)}, b{uint8_t(b)}, a{255} {}
19 MR_BIND_OVERLOAD_SUFFIX(float) constexpr Color( float r, float g, float b, float a ) noexcept :
20 r{ r > 1 ? uint8_t( 255 ) : ( r < 0 ? uint8_t( 0 ) : uint8_t( r * 255 ) )},
21 g{ g > 1 ? uint8_t( 255 ) : ( g < 0 ? uint8_t( 0 ) : uint8_t( g * 255 ) )},
22 b{ b > 1 ? uint8_t( 255 ) : ( b < 0 ? uint8_t( 0 ) : uint8_t( b * 255 ) )},
23 a{ a > 1 ? uint8_t( 255 ) : ( a < 0 ? uint8_t( 0 ) : uint8_t( a * 255 ) )} {}
24 MR_BIND_OVERLOAD_SUFFIX(rgbFloat) constexpr Color( float r, float g, float b ) noexcept : Color( r, g, b, 1.f ) {}
25
26 constexpr unsigned int getUInt32() const noexcept { return ( unsigned( r ) ) + ( unsigned( g ) << 8 ) + ( unsigned( b ) << 16 ) + ( unsigned( a ) << 24 ); }
27
28 static constexpr Color white() noexcept { return Color( 255,255,255,255 ); }
29 static constexpr Color black() noexcept { return Color( 0, 0, 0, 255 ); }
30 static constexpr Color gray() noexcept { return Color( 127, 127, 127, 255 ); }
31 static constexpr Color red() noexcept { return Color( 255, 0, 0, 255 ); }
32 static constexpr Color green() noexcept { return Color( 0, 255, 0, 255 ); }
33 static constexpr Color blue() noexcept { return Color( 0, 0, 255, 255 ); }
34 static constexpr Color yellow() noexcept { return Color( 255, 255, 0, 255 ); }
35 static constexpr Color brown() noexcept { return Color( 135, 74, 43, 255 ); }
36 static constexpr Color purple() noexcept { return Color( 128, 0, 128, 255 ); }
37 static constexpr Color transparent() noexcept { return Color( 0, 0, 0, 0 ); }
38
39 template<typename T>
40 static constexpr uint8_t valToUint8( T val ) noexcept
41 {
42 if constexpr ( std::is_same_v<T, uint8_t> )
43 {
44 return val;
45 }
46 else if constexpr ( std::is_integral_v<T> )
47 {
48 return val >= 255 ? uint8_t( 255 ) : ( val <= 0 ? uint8_t( 0 ) : uint8_t( val ) );
49 }
50 else
51 {
52 return val >= T( 1 ) ? uint8_t( 255 ) : ( val <= T( 0 ) ? uint8_t( 0 ) : uint8_t( val * 255 ) );
53 }
54 }
55
56 template<typename T>
57 explicit constexpr Color( const Vector3<T>& vec ) noexcept :
58 r{ valToUint8( vec.x ) },
59 g{ valToUint8( vec.y ) },
60 b{ valToUint8( vec.z ) },
61 a{ uint8_t( 255 ) }
62 {}
63
64 template<typename T>
65 explicit constexpr Color( const Vector4<T>& vec ) noexcept :
66 r{ valToUint8( vec.x ) },
67 g{ valToUint8( vec.y ) },
68 b{ valToUint8( vec.z ) },
69 a{ valToUint8( vec.w ) }
70 {}
71
72 template<typename T>
73 explicit constexpr operator Vector4<T>() const noexcept
74 {
75 if constexpr ( std::is_integral_v<T> )
76 {
77 return Vector4<T>( T( r ), T( g ), T( b ), T( a ) );
78 }
79 else
80 {
81 return Vector4<T>( T( r ), T( g ), T( b ), T( a ) ) / T( 255 );
82 }
83 }
84
85 const uint8_t& operator []( int e ) const { return *( &r + e ); }
86 uint8_t& operator []( int e ) { return *( &r + e ); }
87
88 Color& operator += ( const Color& other ) { *this = Color( Vector4i( *this ) + Vector4i( other ) ); return *this; }
89 Color& operator -= ( const Color& other ) { *this = Color( Vector4i( *this ) - Vector4i( other ) ); return *this; }
90 Color& operator *= ( float m ) { *this = Color( m * Vector4f( *this ) ); return *this; }
91 Color& operator /= ( float m ) { return *this *= 1.0f / m; }
92
93 constexpr Color scaledAlpha( float m ) const noexcept
94 {
95 return Color( r, g, b, uint8_t( std::clamp( m * a, 0.0f , 255.0f ) ) );
96 }
97
99 [[nodiscard]] constexpr bool isTranslucent() const noexcept { return a > 0 && a < 255; }
100};
101
102inline bool operator ==( const Color& a, const Color& b )
103{
104 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
105}
106
107inline bool operator !=( const Color& a, const Color& b )
108{
109 return !( a == b );
110}
111
112inline Color operator +( const Color& a, const Color& b )
113{
114 return Color(Vector4i( a ) + Vector4i( b ));
115}
116
117inline Color operator -( const Color& a, const Color& b )
118{
119 return Color( Vector4i( a ) - Vector4i( b ) );
120}
121
122inline Color operator *( float a, const Color& b )
123{
124 return Color( a * Vector4f( b ) );
125}
126
127inline Color operator *( const Color& b, float a )
128{
129 return Color( a * Vector4f( b ) );
130}
131
132inline Color operator /( const Color& b, float a )
133{
134 return b * ( 1 / a );
135}
136
139MRMESH_API Color blend( const Color& front, const Color& back );
140
141}
142
143template<>
144struct std::hash<MR::Color>
145{
146 std::uint32_t operator()( MR::Color c ) const noexcept
147 {
148 std::uint32_t r;
149 static_assert( sizeof( r ) == sizeof( c ) );
150 std::memcpy( &r, &c, sizeof( r ) );
151 return r;
152 }
153};
#define MRMESH_API
Definition MRMeshFwd.h:85
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:492
static constexpr Color transparent() noexcept
Definition MRColor.h:37
bool operator==(const Allocator< T > &, const Allocator< U > &) noexcept
Definition MRAllocator.h:95
static constexpr Color green() noexcept
Definition MRColor.h:32
static constexpr Color purple() noexcept
Definition MRColor.h:36
uint8_t a
Definition MRColor.h:13
static constexpr uint8_t valToUint8(T val) noexcept
Definition MRColor.h:40
constexpr Color(const Vector3< T > &vec) noexcept
Definition MRColor.h:57
constexpr Color() noexcept
Definition MRColor.h:15
const uint8_t & operator[](int e) const
Definition MRColor.h:85
constexpr bool isTranslucent() const noexcept
returns true if the color is neither fully opaque nor fully transparent, so it has to be blended with...
Definition MRColor.h:99
Color blend(const Color &front, const Color &back)
Color & operator-=(const Color &other)
Definition MRColor.h:89
bool operator!=(const Color &a, const Color &b)
Definition MRColor.h:107
static constexpr Color black() noexcept
Definition MRColor.h:29
Color operator/(const Color &b, float a)
Definition MRColor.h:132
Vector4
Definition MRMeshFwd.h:205
Color operator*(float a, const Color &b)
Definition MRColor.h:122
static constexpr Color yellow() noexcept
Definition MRColor.h:34
constexpr Color(float r, float g, float b) noexcept
Definition MRColor.h:24
static constexpr Color brown() noexcept
Definition MRColor.h:35
uint8_t g
Definition MRColor.h:13
Color & operator+=(const Color &other)
Definition MRColor.h:88
constexpr unsigned int getUInt32() const noexcept
Using a separate overload instead of a default argument to produce better C bindings.
Definition MRColor.h:26
Color & operator/=(float m)
Definition MRColor.h:91
static constexpr Color red() noexcept
Definition MRColor.h:31
uint8_t r
Definition MRColor.h:13
constexpr Color scaledAlpha(float m) const noexcept
Definition MRColor.h:93
static constexpr Color gray() noexcept
Definition MRColor.h:30
static constexpr Color blue() noexcept
Definition MRColor.h:33
uint8_t b
Definition MRColor.h:13
Color(NoInit) noexcept
Definition MRColor.h:16
constexpr Color(float r, float g, float b, float a) noexcept
Using a separate overload instead of a default argument to produce better C bindings.
Definition MRColor.h:19
constexpr Color(const Vector4< T > &vec) noexcept
Definition MRColor.h:65
Color & operator*=(float m)
Definition MRColor.h:90
constexpr Color(int r, int g, int b) noexcept
Definition MRColor.h:18
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:112
static constexpr Color white() noexcept
Definition MRColor.h:28
@ other
Angle, normally float. Measure in radians.
Definition MRFeatureObject.h:27
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12
Definition MRMeshFwd.h:102
Definition MRVector3.h:33
Definition MRVector4.h:26