diff --git a/src/public/Color.h b/src/public/Color.h index e800a372812..9b4ad6121f2 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -12,6 +12,8 @@ #pragma once #endif +#include "tier1/strtools.h" + //----------------------------------------------------------------------------- // Purpose: Basic handler for an rgb set of colors // This class is fully inline @@ -24,13 +26,139 @@ class Color { *((int *)this) = 0; } - Color(int _r,int _g,int _b) + // These first two support RGB and RGBA in Color255 and Color1 formats. + Color( float _r, float _g, float _b ) + { + int r = (int)_r; + if ( _r < 1 ) + { + r = ( _r * 255.0f ); + } + + int g = (int)_g; + if ( _g < 1 ) + { + g = ( _g * 255.0f ); + } + + int b = (int)_b; + if ( _b < 1 ) + { + b = ( _b * 255.0f ); + } + + SetColor( r, g, b, 0 ); + } + Color( float _r, float _g, float _b, float _a ) + { + int r = (int)_r; + if ( _r < 1 ) + { + r = ( _r * 255.0f ); + } + + int g = (int)_g; + if ( _g < 1 ) + { + g = ( _g * 255.0f ); + } + + int b = (int)_b; + if ( _b < 1 ) + { + b = ( _b * 255.0f ); + } + + int a = (int)_a; + if ( _a < 1 ) + { + a = ( _a * 255.0f ); + } + + SetColor( r, g, b, a ); + } + // These two support hex code strings in 3 digit (without alpha), 6 digit (without alpha) and 9 digit (with alpha) + Color( char *_hexCode ) { - SetColor(_r, _g, _b, 0); + char *col = _hexCode; + + //check if # exists at the beginning. + //if it doesn't, return a raw color of 0. + if ( !Q_strncmp( col, "#", 1 ) ) + { + col = col + 1; + } + else + { + *((int *)this) = 0; + return; + } + + int r = 0; + int g = 0; + int b = 0; + int a = 0; + + if ( Q_strlen(col) == 3 ) + { + r = V_nibble( col[0] ) << 4; + g = V_nibble( col[1] ) << 4; + b = V_nibble( col[2] ) << 4; + } + else + { + r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); + g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); + b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); + + if ( col[6] && col[7] ) + { + a = V_nibble( col[6]) << 4 | V_nibble(col[7] ); + } + } + + SetColor( r, g, b, a ); } - Color(int _r,int _g,int _b,int _a) + Color( wchar_t *_hexCode ) { - SetColor(_r, _g, _b, _a); + wchar_t *col = _hexCode; + + //check if # exists at the beginning. + //if it doesn't, return a raw color of 0. + if ( !wcsncmp( col, L"#", 1 ) ) + { + col = col + 1; + } + else + { + *((int *)this) = 0; + return; + } + + int r = 0; + int g = 0; + int b = 0; + int a = 0; + + if ( wcslen( col ) == 3 ) + { + r = V_nibble( col[0] ) << 4; + g = V_nibble( col[1] ) << 4; + b = V_nibble( col[2] ) << 4; + } + else + { + r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); + g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); + b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); + + if ( col[6] && col[7] ) + { + a = V_nibble( col[6] ) << 4 | V_nibble( col[7] ); + } + } + + SetColor( r, g, b, a ); } // set the color diff --git a/src/tier1/KeyValues.cpp b/src/tier1/KeyValues.cpp index 648b4522232..8c419b8dec6 100644 --- a/src/tier1/KeyValues.cpp +++ b/src/tier1/KeyValues.cpp @@ -1547,6 +1547,17 @@ Color KeyValues::GetColor( const char *keyName ) KeyValues *dat = FindKey( keyName, false ); if ( dat ) { + //first, check if it's a hex code before we move on to check its type. + if ( dat->m_iDataType == TYPE_STRING ) + { + Color testCol = Color( dat->m_sValue ); + if ( testCol.GetRawColor() != 0 ) + { + color = testCol; + return color; + } + } + if ( dat->m_iDataType == TYPE_COLOR ) { color[0] = dat->m_Color[0];