38class ColorBase :
public Eigen::Matrix<_Scalar, 4, 1>
41 using VectorType = Eigen::Matrix<_Scalar, 4, 1>;
43 explicit inline ColorBase() :
ColorBase( _Scalar( 1. ), _Scalar( 1. ), _Scalar( 1. ) ) {}
45 template <
typename S2>
46 inline ColorBase( S2 r, S2 g, S2 b, S2 alpha = S2( 1 ) ) :
47 VectorType( _Scalar( r ), _Scalar( g ), _Scalar( b ), _Scalar( alpha ) ) {}
50 template <
typename S2>
54 template <
typename Derived>
55 inline ColorBase(
const Eigen::MatrixBase<Derived>& v ) :
56 VectorType( v.template cast<_Scalar>() ) {}
59 operator VectorType() {
return *
this; }
64 for (
auto& u : c.rgb() ) {
65 if ( u < 0.04045_ra ) { u /= 12.92_ra; }
66 else { u =
std::pow( ( u + 0.055_ra ) / 1.055_ra, 2.4_ra ); }
74 for (
auto& u : c.rgb() ) {
75 if ( u < 0.0031308_ra ) { u *= 12.92_ra; }
76 else { u = 1.055_ra *
std::pow( u, 1_ra / 2.4_ra ) - 0.055_ra; }
81 template <
typename Derived>
82 static inline ColorBase fromRGB(
const Eigen::MatrixBase<Derived>& rgb,
83 Scalar alpha = Scalar( 1. ) ) {
84 ColorBase c( rgb.template cast<_Scalar>().homogeneous() );
89 Eigen::Block<VectorType, 3, 1> rgb() {
return ( *this ).template head<3>(); }
90 const Eigen::Block<VectorType, 3, 1> rgb()
const {
return ( *this ).template head<3>(); }
92 Scalar alpha()
const {
return ( *
this )( 3 ); }
93 Scalar& alpha() {
return ( *
this )( 3 ); }
94 bool hasValidAlpha()
const {
return Math::checkRange( alpha(), 0_ra, 1_ra ); }
96 static inline ColorBase<_Scalar> Alpha() {
97 return ColorBase<_Scalar>( _Scalar( 0. ), _Scalar( 0. ), _Scalar( 0. ), _Scalar( 0. ) );
100 static inline ColorBase<_Scalar> Black() {
101 return ColorBase<_Scalar>( _Scalar( 0. ), _Scalar( 0. ), _Scalar( 0. ) );
104 static inline ColorBase<_Scalar> Red() {
105 return ColorBase<_Scalar>( _Scalar( 1. ), _Scalar( 0. ), _Scalar( 0. ) );
108 static inline ColorBase<_Scalar> Green() {
109 return ColorBase<_Scalar>( _Scalar( 0. ), _Scalar( 1. ), _Scalar( 0. ) );
112 static inline ColorBase<_Scalar> Blue() {
113 return ColorBase<_Scalar>( _Scalar( 0. ), _Scalar( 0. ), _Scalar( 1. ) );
116 static inline ColorBase<_Scalar> Yellow() {
117 return ColorBase<_Scalar>( _Scalar( 1. ), _Scalar( 1. ), _Scalar( 0. ) );
120 static inline ColorBase<_Scalar> Magenta() {
121 return ColorBase<_Scalar>( _Scalar( 1. ), _Scalar( 0. ), _Scalar( 1. ) );
124 static inline ColorBase<_Scalar> Cyan() {
125 return ColorBase<_Scalar>( _Scalar( 0. ), _Scalar( 1. ), _Scalar( 1. ) );
128 static inline ColorBase<_Scalar> White() {
129 return ColorBase<_Scalar>( _Scalar( 1. ), _Scalar( 1. ), _Scalar( 1. ) );
132 static inline ColorBase<_Scalar> Grey( _Scalar f = _Scalar( 0.5 ), _Scalar a = _Scalar( 1. ) ) {
133 return ColorBase<_Scalar>( f, f, f, a );
136 static inline ColorBase<_Scalar> Skin() {
137 return ColorBase<_Scalar>( _Scalar( 1.0 ), _Scalar( 0.87 ), _Scalar( 0.74 ) );
145 return fromRGB24( getNamedColorCode( name ) );
150 static inline ColorBase<_Scalar> fromChars( uchar r, uchar g, uchar b, uchar a = 0xff ) {
152 _Scalar( g ) / 255.0f,
153 _Scalar( b ) / 255.0f,
154 _Scalar( a ) / 255.0f );
158 uchar r = uchar( ( rgb >> 16 ) & 0xff );
159 uchar g = uchar( ( rgb >> 8 ) & 0xff );
160 uchar b = uchar( ( rgb >> 0 ) & 0xff );
161 return fromChars( r, g, b );
165 uchar r = uchar( ( rgba >> 24 ) & 0xff );
166 uchar g = uchar( ( rgba >> 16 ) & 0xff );
167 uchar b = uchar( ( rgba >> 8 ) & 0xff );
168 uchar a = uchar( ( rgba >> 0 ) & 0xff );
169 return fromChars( r, g, b, a );
172 static inline ColorBase<_Scalar> fromARGB32( uint32_t argb ) {
173 uchar a = uchar( ( argb >> 24 ) & 0xff );
174 uchar r = uchar( ( argb >> 16 ) & 0xff );
175 uchar g = uchar( ( argb >> 8 ) & 0xff );
176 uchar b = uchar( ( argb >> 0 ) & 0xff );
177 return fromChars( r, g, b, a );
180 static inline ColorBase<_Scalar> fromHSV(
const _Scalar hue,
181 const _Scalar saturation = 1.0,
182 const _Scalar value = 1.0,
183 const _Scalar alpha = 1.0 ) {
184 ColorBase<_Scalar> c;
186 if ( saturation == 0.0f ) {
187 c[0] = c[1] = c[2] = value;
191 _Scalar h = ( ( hue == 1.0f ) ? 0.0f : hue ) * 6.0f;
193 _Scalar v1 = value * ( 1.0f - saturation );
194 _Scalar v2 = value * ( 1.0f - ( saturation * ( h - i ) ) );
195 _Scalar v3 = value * ( 1.0f - ( saturation * ( 1.0f - h - i ) ) );
232 inline uint32_t toRGBA32()
const {
233 ColorBase<_Scalar> c( ( *
this ) * 255 );
234 Eigen::Matrix<int, 4, 1> scaled( c.x(), c.y(), c.z(), c.w() );
235 return ( uint32_t( scaled( 0 ) ) << 24 ) | ( uint32_t( scaled( 1 ) ) << 16 ) |
236 ( uint32_t( scaled( 2 ) ) << 8 ) | ( uint32_t( scaled( 3 ) ) << 0 );
239 inline uint32_t toARGB32()
const {
240 ColorBase<_Scalar> c( ( *
this ) * 255 );
241 Eigen::Matrix<int, 4, 1> scaled( c.x(), c.y(), c.z(), c.w() );
242 return ( uint32_t( scaled( 3 ) ) << 24 ) | ( uint32_t( scaled( 0 ) ) << 16 ) |
243 ( uint32_t( scaled( 1 ) ) << 8 ) | ( uint32_t( scaled( 2 ) ) << 0 );
249 for ( uint i = 0; i < size; ++i ) {
250 color[i] = fromHSV( ( _Scalar( i ) / _Scalar( size - 1 ) ) * 0.777 );
251 color[i] = ( color[i] + ColorBase<_Scalar>::Constant( gamma ) ) * 0.5;
253 else { color[0] = Red(); }