Skip to content

[ALL] Allow the Color class to accept hex code strings and Color1 variables as colors + Basic KeyValues support. #1288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
136 changes: 132 additions & 4 deletions src/public/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/tier1/KeyValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down