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 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 constexpr Color( int r, int g, int b ) noexcept : r{uint8_t(r)}, g{uint8_t(g)}, b{uint8_t(b)}, a{255} {}
19 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 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};
98
99inline bool operator ==( const Color& a, const Color& b )
100{
101 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
102}
103
104inline bool operator !=( const Color& a, const Color& b )
105{
106 return !( a == b );
107}
108
109inline Color operator +( const Color& a, const Color& b )
110{
111 return Color(Vector4i( a ) + Vector4i( b ));
112}
113
114inline Color operator -( const Color& a, const Color& b )
115{
116 return Color( Vector4i( a ) - Vector4i( b ) );
117}
118
119inline Color operator *( float a, const Color& b )
120{
121 return Color( a * Vector4f( b ) );
122}
123
124inline Color operator *( const Color& b, float a )
125{
126 return Color( a * Vector4f( b ) );
127}
128
129inline Color operator /( const Color& b, float a )
130{
131 return b * ( 1 / a );
132}
133
136MRMESH_API Color blend( const Color& front, const Color& back );
137
138}
139
140template<>
141struct std::hash<MR::Color>
142{
143 std::uint32_t operator()( MR::Color c ) const noexcept
144 {
145 std::uint32_t r;
146 static_assert( sizeof( r ) == sizeof( c ) );
147 std::memcpy( &r, &c, sizeof( r ) );
148 return r;
149 }
150};
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
static constexpr Color transparent() noexcept
Definition MRColor.h:37
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
Color & operator-=(const Color &other)
Definition MRColor.h:89
bool operator!=(const Color &a, const Color &b)
Definition MRColor.h:104
static constexpr Color black() noexcept
Definition MRColor.h:29
constexpr Color(int r, int g, int b, int a) noexcept
Definition MRColor.h:17
Color operator/(const Color &b, float a)
Definition MRColor.h:129
Color operator*(float a, const Color &b)
Definition MRColor.h:119
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
MRMESH_API Color blend(const Color &front, const Color &back)
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:109
static constexpr Color white() noexcept
Definition MRColor.h:28
@ other
Angle, normally float. Measure in radians.
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12
Definition MRVector3.h:33
Definition MRVector4.h:26