MeshLib
 
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{
8struct Color
9{
10 uint8_t r, g, b, a;
11
12 constexpr Color() noexcept : r{ 0 }, g{ 0 }, b{ 0 }, a{ 255 } {}
13 explicit Color( NoInit ) noexcept {}
14 constexpr Color( int r, int g, int b, int a = 255 ) noexcept :
15 r{uint8_t(r)},
16 g{uint8_t(g)},
17 b{uint8_t(b)},
18 a{uint8_t(a)} {}
19 constexpr Color( float r, float g, float b, float a = 1 ) 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
25 constexpr unsigned int getUInt32() const noexcept { return ( unsigned( r ) ) + ( unsigned( g ) << 8 ) + ( unsigned( b ) << 16 ) + ( unsigned( a ) << 24 ); }
26
27 static constexpr Color white() noexcept { return Color( 255,255,255,255 ); }
28 static constexpr Color black() noexcept { return Color( 0, 0, 0, 255 ); }
29 static constexpr Color gray() noexcept { return Color( 127, 127, 127, 255 ); }
30 static constexpr Color red() noexcept { return Color( 255, 0, 0, 255 ); }
31 static constexpr Color green() noexcept { return Color( 0, 255, 0, 255 ); }
32 static constexpr Color blue() noexcept { return Color( 0, 0, 255, 255 ); }
33 static constexpr Color yellow() noexcept { return Color( 255, 255, 0, 255 ); }
34 static constexpr Color brown() noexcept { return Color( 135, 74, 43, 255 ); }
35 static constexpr Color purple() noexcept { return Color( 128, 0, 128, 255 ); }
36 static constexpr Color transparent() noexcept { return Color( 0, 0, 0, 0 ); }
37
38 template<typename T>
39 static constexpr uint8_t valToUint8( T val ) noexcept
40 {
41 if constexpr ( std::is_same_v<T, uint8_t> )
42 {
43 return val;
44 }
45 else if constexpr ( std::is_integral_v<T> )
46 {
47 return val >= 255 ? uint8_t( 255 ) : ( val <= 0 ? uint8_t( 0 ) : uint8_t( val ) );
48 }
49 else
50 {
51 return val >= T( 1 ) ? uint8_t( 255 ) : ( val <= T( 0 ) ? uint8_t( 0 ) : uint8_t( val * 255 ) );
52 }
53 }
54
55 template<typename T>
56 explicit constexpr Color( const Vector3<T>& vec ) noexcept :
57 r{ valToUint8( vec.x ) },
58 g{ valToUint8( vec.y ) },
59 b{ valToUint8( vec.z ) },
60 a{ uint8_t( 255 ) }
61 {}
62
63 template<typename T>
64 explicit constexpr Color( const Vector4<T>& vec ) noexcept :
65 r{ valToUint8( vec.x ) },
66 g{ valToUint8( vec.y ) },
67 b{ valToUint8( vec.z ) },
68 a{ valToUint8( vec.w ) }
69 {}
70
71 template<typename T>
72 explicit constexpr operator Vector4<T>() const noexcept
73 {
74 if constexpr ( std::is_integral_v<T> )
75 {
76 return Vector4<T>( T( r ), T( g ), T( b ), T( a ) );
77 }
78 else
79 {
80 return Vector4<T>( T( r ), T( g ), T( b ), T( a ) ) / T( 255 );
81 }
82 }
83
84 const uint8_t& operator []( int e ) const { return *( &r + e ); }
85 uint8_t& operator []( int e ) { return *( &r + e ); }
86
87 Color& operator += ( const Color& other ) { *this = Color( Vector4i( *this ) + Vector4i( other ) ); return *this; }
88 Color& operator -= ( const Color& other ) { *this = Color( Vector4i( *this ) - Vector4i( other ) ); return *this; }
89 Color& operator *= ( float m ) { *this = Color( m * Vector4f( *this ) ); return *this; }
90 Color& operator /= ( float m ) { return *this *= 1.0f / m; }
91
92 constexpr Color scaledAlpha( float m ) const noexcept
93 {
94 return Color( r, g, b, uint8_t( std::clamp( m * a, 0.0f , 255.0f ) ) );
95 }
96};
97
98inline bool operator ==( const Color& a, const Color& b )
99{
100 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
101}
102
103inline bool operator !=( const Color& a, const Color& b )
104{
105 return !( a == b );
106}
107
108inline Color operator +( const Color& a, const Color& b )
109{
110 return Color(Vector4i( a ) + Vector4i( b ));
111}
112
113inline Color operator -( const Color& a, const Color& b )
114{
115 return Color( Vector4i( a ) - Vector4i( b ) );
116}
117
118inline Color operator *( float a, const Color& b )
119{
120 return Color( a * Vector4f( b ) );
121}
122
123inline Color operator *( const Color& b, float a )
124{
125 return Color( a * Vector4f( b ) );
126}
127
128inline Color operator /( const Color& b, float a )
129{
130 return b * ( 1 / a );
131}
132
135MRMESH_API Color blend( const Color& front, const Color& back );
136
137}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
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...
bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:259
Definition MRCameraOrientationPlugin.h:7
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRColor.h:118
MRMESH_API Color blend(const Color &front, const Color &back)
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRColor.h:9
static constexpr Color transparent() noexcept
Definition MRColor.h:36
static constexpr Color green() noexcept
Definition MRColor.h:31
static constexpr Color purple() noexcept
Definition MRColor.h:35
uint8_t a
Definition MRColor.h:10
static constexpr uint8_t valToUint8(T val) noexcept
Definition MRColor.h:39
constexpr Color(const Vector3< T > &vec) noexcept
Definition MRColor.h:56
constexpr Color() noexcept
Definition MRColor.h:12
const uint8_t & operator[](int e) const
Definition MRColor.h:84
Color & operator-=(const Color &other)
Definition MRColor.h:88
static constexpr Color black() noexcept
Definition MRColor.h:28
constexpr Color(float r, float g, float b, float a=1) noexcept
Definition MRColor.h:19
static constexpr Color yellow() noexcept
Definition MRColor.h:33
static constexpr Color brown() noexcept
Definition MRColor.h:34
uint8_t g
Definition MRColor.h:10
Color & operator+=(const Color &other)
Definition MRColor.h:87
constexpr unsigned int getUInt32() const noexcept
Definition MRColor.h:25
Color & operator/=(float m)
Definition MRColor.h:90
static constexpr Color red() noexcept
Definition MRColor.h:30
uint8_t r
Definition MRColor.h:10
constexpr Color(int r, int g, int b, int a=255) noexcept
Definition MRColor.h:14
constexpr Color scaledAlpha(float m) const noexcept
Definition MRColor.h:92
static constexpr Color gray() noexcept
Definition MRColor.h:29
static constexpr Color blue() noexcept
Definition MRColor.h:32
uint8_t b
Definition MRColor.h:10
Color(NoInit) noexcept
Definition MRColor.h:13
constexpr Color(const Vector4< T > &vec) noexcept
Definition MRColor.h:64
Color & operator*=(float m)
Definition MRColor.h:89
static constexpr Color white() noexcept
Definition MRColor.h:27
Definition MRMesh/MRMeshFwd.h:56
Definition MRMesh/MRVector3.h:19
Definition MRVector4.h:13