2019-10-05 17:28:05 +00:00
# pragma once
2019-11-15 19:51:02 +00:00
# include <algorithm>
2019-10-05 17:28:05 +00:00
# include <stdint.h>
2020-04-11 21:48:14 +00:00
// Beware of windows.h :(
2019-12-22 19:55:47 +00:00
# ifdef max
# undef min
# undef max
# endif
2019-10-05 17:28:05 +00:00
struct PalEntry
{
PalEntry ( ) = default ;
2021-11-14 11:30:18 +00:00
PalEntry ( const PalEntry & ) = default ;
2020-07-06 19:57:38 +00:00
constexpr PalEntry ( uint32_t argb ) : d ( argb ) { }
2019-10-05 17:28:05 +00:00
operator uint32_t ( ) const { return d ; }
void SetRGB ( PalEntry other )
{
d = other . d & 0xffffff ;
}
PalEntry Modulate ( PalEntry other ) const
{
if ( isWhite ( ) )
{
return other ;
}
else if ( other . isWhite ( ) )
{
return * this ;
}
else
{
other . r = ( r * other . r ) / 255 ;
other . g = ( g * other . g ) / 255 ;
other . b = ( b * other . b ) / 255 ;
return other ;
}
}
2020-07-06 19:57:38 +00:00
constexpr int Luminance ( ) const
2019-10-05 17:28:05 +00:00
{
return ( r * 77 + g * 143 + b * 37 ) > > 8 ;
}
2020-07-06 19:57:38 +00:00
constexpr int Amplitude ( ) const
2019-11-15 19:51:02 +00:00
{
return std : : max ( r , std : : max ( g , b ) ) ;
}
2020-07-06 19:57:38 +00:00
constexpr void Decolorize ( ) // this for 'nocoloredspritelighting' and not the same as desaturation. The normal formula results in a value that's too dark.
2019-10-05 17:28:05 +00:00
{
int v = ( r + g + b ) ;
r = g = b = ( ( 255 * 3 ) + v + v ) / 9 ;
}
2020-07-06 19:57:38 +00:00
constexpr bool isBlack ( ) const
2019-10-05 17:28:05 +00:00
{
return ( d & 0xffffff ) = = 0 ;
}
2020-07-06 19:57:38 +00:00
constexpr bool isWhite ( ) const
2019-10-05 17:28:05 +00:00
{
return ( d & 0xffffff ) = = 0xffffff ;
}
PalEntry & operator = ( const PalEntry & other ) = default ;
2020-07-06 19:57:38 +00:00
constexpr PalEntry & operator = ( uint32_t other ) { d = other ; return * this ; }
constexpr PalEntry InverseColor ( ) const { PalEntry nc ( a , 255 - r , 255 - g , 255 - b ) ; return nc ; }
2019-10-05 17:28:05 +00:00
# ifdef __BIG_ENDIAN__
2020-07-06 19:57:38 +00:00
constexpr PalEntry ( uint8_t ir , uint8_t ig , uint8_t ib ) : a ( 0 ) , r ( ir ) , g ( ig ) , b ( ib ) { }
constexpr PalEntry ( uint8_t ia , uint8_t ir , uint8_t ig , uint8_t ib ) : a ( ia ) , r ( ir ) , g ( ig ) , b ( ib ) { }
2019-10-05 17:28:05 +00:00
union
{
struct
{
uint8_t a , r , g , b ;
} ;
uint32_t d ;
} ;
# else
2020-07-06 19:57:38 +00:00
constexpr PalEntry ( uint8_t ir , uint8_t ig , uint8_t ib ) : b ( ib ) , g ( ig ) , r ( ir ) , a ( 0 ) { }
constexpr PalEntry ( uint8_t ia , uint8_t ir , uint8_t ig , uint8_t ib ) : b ( ib ) , g ( ig ) , r ( ir ) , a ( ia ) { }
2019-10-05 17:28:05 +00:00
union
{
struct
{
uint8_t b , g , r , a ;
} ;
uint32_t d ;
} ;
# endif
} ;
2020-07-06 19:57:38 +00:00
constexpr inline int Luminance ( int r , int g , int b )
2019-10-05 17:28:05 +00:00
{
return ( r * 77 + g * 143 + b * 37 ) > > 8 ;
}
2019-10-23 23:20:58 +00:00
# define APART(c) (((c)>>24)&0xff)
# define RPART(c) (((c)>>16)&0xff)
# define GPART(c) (((c)>>8)&0xff)
# define BPART(c) ((c)&0xff)
# define MAKERGB(r,g,b) uint32_t(((r)<<16)|((g)<<8)|(b))
# define MAKEARGB(a,r,g,b) uint32_t(((a)<<24)|((r)<<16)|((g)<<8)|(b))