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