From 85dc90df8e6b11b5507350f3cda6be9ab2153c83 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 13:04:06 -0700 Subject: [PATCH 01/12] [ALL} Allow the Color class to accept hex code strings as colors. --- src/public/Color.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/public/Color.h b/src/public/Color.h index e800a372812..352cb9d27aa 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 @@ -32,6 +34,20 @@ class Color { SetColor(_r, _g, _b, _a); } + Color(const char *_hexCode, bool alphaHex = false) + { + int r = V_nibble(_hexCode[0]) << 4 | V_nibble(_hexCode[1]); + int g = V_nibble(_hexCode[2]) << 4 | V_nibble(_hexCode[3]); + int b = V_nibble(_hexCode[4]) << 4 | V_nibble(_hexCode[5]); + int a = 255; + + if (alphaHex) + { + a = V_nibble(_hexCode[6]) << 4 | V_nibble(_hexCode[7]); + } + + SetColor(r, g, b, a); + } // set the color // r - red component (0-255) From 05ccbf993e34695cc00dafa80cc47c33781bdd8e Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 13:18:59 -0700 Subject: [PATCH 02/12] improve formatting, add better alpha support --- src/public/Color.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index 352cb9d27aa..e5bbef2a664 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -34,19 +34,19 @@ class Color { SetColor(_r, _g, _b, _a); } - Color(const char *_hexCode, bool alphaHex = false) + Color(const char *_hexCode) { - int r = V_nibble(_hexCode[0]) << 4 | V_nibble(_hexCode[1]); - int g = V_nibble(_hexCode[2]) << 4 | V_nibble(_hexCode[3]); - int b = V_nibble(_hexCode[4]) << 4 | V_nibble(_hexCode[5]); - int a = 255; + int r = V_nibble( _hexCode[0] ) << 4 | V_nibble( _hexCode[1] ); + int g = V_nibble( _hexCode[2] ) << 4 | V_nibble( _hexCode[3] ); + int b = V_nibble( _hexCode[4] ) << 4 | V_nibble( _hexCode[5] ); + int a = 0; - if (alphaHex) + if ( _hexCode[6] && _hexCode[7] ) { - a = V_nibble(_hexCode[6]) << 4 | V_nibble(_hexCode[7]); + a = V_nibble( _hexCode[6] ) << 4 | V_nibble( _hexCode[7] ); } - SetColor(r, g, b, a); + SetColor( r, g, b, a ); } // set the color From 3daa4a4a5f693f1da56f55b7d513294765515a03 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 13:39:03 -0700 Subject: [PATCH 03/12] Added a wchar_t version --- src/public/Color.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/public/Color.h b/src/public/Color.h index e5bbef2a664..033c826dc12 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -34,7 +34,21 @@ class Color { SetColor(_r, _g, _b, _a); } - Color(const char *_hexCode) + Color( char *_hexCode ) + { + int r = V_nibble( _hexCode[0] ) << 4 | V_nibble( _hexCode[1] ); + int g = V_nibble( _hexCode[2] ) << 4 | V_nibble( _hexCode[3] ); + int b = V_nibble( _hexCode[4] ) << 4 | V_nibble( _hexCode[5] ); + int a = 0; + + if ( _hexCode[6] && _hexCode[7] ) + { + a = V_nibble( _hexCode[6] ) << 4 | V_nibble( _hexCode[7] ); + } + + SetColor( r, g, b, a ); + } + Color( wchar_t *_hexCode ) { int r = V_nibble( _hexCode[0] ) << 4 | V_nibble( _hexCode[1] ); int g = V_nibble( _hexCode[2] ) << 4 | V_nibble( _hexCode[3] ); From 6904aa394e40649a20e87a6f6337cfb60aeab463 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 14:15:02 -0700 Subject: [PATCH 04/12] add syntax removal --- src/public/Color.h | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index 033c826dc12..083a7b9903e 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -36,28 +36,42 @@ class Color } Color( char *_hexCode ) { - int r = V_nibble( _hexCode[0] ) << 4 | V_nibble( _hexCode[1] ); - int g = V_nibble( _hexCode[2] ) << 4 | V_nibble( _hexCode[3] ); - int b = V_nibble( _hexCode[4] ) << 4 | V_nibble( _hexCode[5] ); + char *col = _hexCode; + + if ( col[8] ) + { + col = col + 1; + } + + int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); + int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); + int b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); int a = 0; - if ( _hexCode[6] && _hexCode[7] ) + if ( col[6] && col[7] ) { - a = V_nibble( _hexCode[6] ) << 4 | V_nibble( _hexCode[7] ); + a = V_nibble( col[6] ) << 4 | V_nibble( col[7] ); } SetColor( r, g, b, a ); } Color( wchar_t *_hexCode ) { - int r = V_nibble( _hexCode[0] ) << 4 | V_nibble( _hexCode[1] ); - int g = V_nibble( _hexCode[2] ) << 4 | V_nibble( _hexCode[3] ); - int b = V_nibble( _hexCode[4] ) << 4 | V_nibble( _hexCode[5] ); + wchar_t *col = _hexCode; + + if ( col[8] ) + { + col = col + 1; + } + + int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); + int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); + int b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); int a = 0; - if ( _hexCode[6] && _hexCode[7] ) + if ( col[6] && col[7] ) { - a = V_nibble( _hexCode[6] ) << 4 | V_nibble( _hexCode[7] ); + a = V_nibble( col[6] ) << 4 | V_nibble( col[7] ); } SetColor( r, g, b, a ); From 6257e59b6ffcb34cc8357beedd46bd3f1ecebc51 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 15:01:00 -0700 Subject: [PATCH 05/12] implement null pointer fallback --- src/public/Color.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/public/Color.h b/src/public/Color.h index 083a7b9903e..8638d54bc37 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -37,6 +37,14 @@ class Color Color( char *_hexCode ) { char *col = _hexCode; + + //check if FF or #F exist at the beginning. + //if they don't, return a raw color of 0. + if ( !col[0] && !col[1] ) + { + *( ( int* )this ) = 0; + return; + } if ( col[8] ) { @@ -58,6 +66,14 @@ class Color Color( wchar_t *_hexCode ) { wchar_t *col = _hexCode; + + //check if FF or #F exist at the beginning. + //if they don't, return a raw color of 0. + if ( !col[0] && !col[1] ) + { + *( ( int* )this ) = 0; + return; + } if ( col[8] ) { From a6b19c5eb84271d015e18c0e2728b1d49ea7be4c Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 15:13:45 -0700 Subject: [PATCH 06/12] add basic KeyValues support --- src/tier1/KeyValues.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) 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]; From 59eb52dc237634cb696cce7445d448b79ce2dd21 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 15:29:47 -0700 Subject: [PATCH 07/12] make null pointer check better --- src/public/Color.h | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index 8638d54bc37..cd838dac8ea 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -38,18 +38,17 @@ class Color { char *col = _hexCode; - //check if FF or #F exist at the beginning. - //if they don't, return a raw color of 0. - if ( !col[0] && !col[1] ) - { - *( ( int* )this ) = 0; - return; - } - + //check if # exists at the beginning. + //if it doesn't, return a raw color of 0. if ( col[8] ) { col = col + 1; } + else + { + *((int *)this) = 0; + return; + } int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); @@ -67,18 +66,17 @@ class Color { wchar_t *col = _hexCode; - //check if FF or #F exist at the beginning. - //if they don't, return a raw color of 0. - if ( !col[0] && !col[1] ) - { - *( ( int* )this ) = 0; - return; - } - + //check if # exists at the beginning. + //if it doesn't, return a raw color of 0. if ( col[8] ) { col = col + 1; } + else + { + *((int *)this) = 0; + return; + } int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); From aae15b51890d517ca91bc4dd4e8a31b02e19ccf0 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 15:44:20 -0700 Subject: [PATCH 08/12] fix normal hex code support --- src/public/Color.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index cd838dac8ea..a10d10089a2 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -40,7 +40,7 @@ class Color //check if # exists at the beginning. //if it doesn't, return a raw color of 0. - if ( col[8] ) + if ( col[8] || col[6] ) { col = col + 1; } @@ -68,7 +68,7 @@ class Color //check if # exists at the beginning. //if it doesn't, return a raw color of 0. - if ( col[8] ) + if ( col[8] || col[6] ) { col = col + 1; } From 0a39e161da9265e77da4da33578492ac3f6204c1 Mon Sep 17 00:00:00 2001 From: Bitl Date: Sat, 17 May 2025 16:02:08 -0700 Subject: [PATCH 09/12] replace char check with string comparison --- src/public/Color.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index a10d10089a2..7b24870b9eb 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -40,7 +40,7 @@ class Color //check if # exists at the beginning. //if it doesn't, return a raw color of 0. - if ( col[8] || col[6] ) + if ( !Q_strncmp( col, "#", 1 ) ) { col = col + 1; } @@ -68,7 +68,7 @@ class Color //check if # exists at the beginning. //if it doesn't, return a raw color of 0. - if ( col[8] || col[6] ) + if ( !wcsncmp( col, L"#", 1 ) ) { col = col + 1; } From 60b2bf4dd248870c1c1ec35e4233e5c7804c3dab Mon Sep 17 00:00:00 2001 From: Bitl Date: Sun, 18 May 2025 16:27:50 -0700 Subject: [PATCH 10/12] Add color 1 support --- src/public/Color.h | 52 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index 7b24870b9eb..ddd6ffa5859 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -26,14 +26,58 @@ 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 ) { - SetColor(_r, _g, _b, 0); + 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(int _r,int _g,int _b,int _a) + Color( float _r, float _g, float _b, float _a ) { - SetColor(_r, _g, _b, _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 6 digit (without alpha) and 9 digit (with alpha) Color( char *_hexCode ) { char *col = _hexCode; From ec5888305bcd24a78c58d30e23aa718164f3872b Mon Sep 17 00:00:00 2001 From: Bitl Date: Sun, 18 May 2025 16:30:32 -0700 Subject: [PATCH 11/12] fix values not being proper --- src/public/Color.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index ddd6ffa5859..5232c28ed3d 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -30,19 +30,19 @@ class Color Color( float _r, float _g, float _b ) { int r = (int)_r; - if ( r < 1 ) + if ( _r < 1 ) { r = ( _r * 255.0f ); } int g = (int)_g; - if ( g < 1 ) + if ( _g < 1 ) { g = ( _g * 255.0f ); } int b = (int)_b; - if ( b < 1 ) + if ( _b < 1 ) { b = ( _b * 255.0f ); } @@ -52,25 +52,25 @@ class Color Color( float _r, float _g, float _b, float _a ) { int r = (int)_r; - if ( r < 1 ) + if ( _r < 1 ) { r = ( _r * 255.0f ); } int g = (int)_g; - if ( g < 1 ) + if ( _g < 1 ) { g = ( _g * 255.0f ); } int b = (int)_b; - if ( b < 1 ) + if ( _b < 1 ) { b = ( _b * 255.0f ); } int a = (int)_a; - if ( a < 1 ) + if ( _a < 1 ) { a = ( _a * 255.0f ); } From 339cc110735ed54e99b7b188de444f51bf879d0f Mon Sep 17 00:00:00 2001 From: Bitl Date: Sun, 18 May 2025 16:56:58 -0700 Subject: [PATCH 12/12] fix 3 digit support --- src/public/Color.h | 52 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/public/Color.h b/src/public/Color.h index 5232c28ed3d..9b4ad6121f2 100644 --- a/src/public/Color.h +++ b/src/public/Color.h @@ -77,11 +77,11 @@ class Color SetColor( r, g, b, a ); } - // These two support hex code strings in 6 digit (without alpha) and 9 digit (with alpha) + // These two support hex code strings in 3 digit (without alpha), 6 digit (without alpha) and 9 digit (with alpha) Color( char *_hexCode ) { char *col = _hexCode; - + //check if # exists at the beginning. //if it doesn't, return a raw color of 0. if ( !Q_strncmp( col, "#", 1 ) ) @@ -94,14 +94,27 @@ class Color return; } - int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); - int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); - int b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); + int r = 0; + int g = 0; + int b = 0; int a = 0; - if ( col[6] && col[7] ) + if ( Q_strlen(col) == 3 ) + { + r = V_nibble( col[0] ) << 4; + g = V_nibble( col[1] ) << 4; + b = V_nibble( col[2] ) << 4; + } + else { - a = V_nibble( col[6] ) << 4 | V_nibble( col[7] ); + 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 ); @@ -109,7 +122,7 @@ class Color Color( wchar_t *_hexCode ) { 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 ) ) @@ -122,14 +135,27 @@ class Color return; } - int r = V_nibble( col[0] ) << 4 | V_nibble( col[1] ); - int g = V_nibble( col[2] ) << 4 | V_nibble( col[3] ); - int b = V_nibble( col[4] ) << 4 | V_nibble( col[5] ); + int r = 0; + int g = 0; + int b = 0; int a = 0; - if ( col[6] && col[7] ) + if ( wcslen( col ) == 3 ) + { + r = V_nibble( col[0] ) << 4; + g = V_nibble( col[1] ) << 4; + b = V_nibble( col[2] ) << 4; + } + else { - a = V_nibble( col[6] ) << 4 | V_nibble( col[7] ); + 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 );