Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.3 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef COLOR_H
  8. #define COLOR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier0/basetypes.h"
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Basic handler for an rgb set of colors
  15. // This class is fully inline
  16. //-----------------------------------------------------------------------------
  17. class Color
  18. {
  19. public:
  20. // constructors
  21. Color()
  22. {
  23. *((int *)this) = 0;
  24. }
  25. Color(int _r,int _g,int _b)
  26. {
  27. SetColor(_r, _g, _b, 0);
  28. }
  29. Color(int _r,int _g,int _b,int _a)
  30. {
  31. SetColor(_r, _g, _b, _a);
  32. }
  33. // set the color
  34. // r - red component (0-255)
  35. // g - green component (0-255)
  36. // b - blue component (0-255)
  37. // a - alpha component, controls transparency (0 - transparent, 255 - opaque);
  38. void SetColor(int _r, int _g, int _b, int _a = 0)
  39. {
  40. _color[0] = (unsigned char)_r;
  41. _color[1] = (unsigned char)_g;
  42. _color[2] = (unsigned char)_b;
  43. _color[3] = (unsigned char)_a;
  44. }
  45. void GetColor(int &_r, int &_g, int &_b, int &_a) const
  46. {
  47. _r = _color[0];
  48. _g = _color[1];
  49. _b = _color[2];
  50. _a = _color[3];
  51. }
  52. void SetRawColor( int color32 )
  53. {
  54. *((int *)this) = color32;
  55. }
  56. int GetRawColor() const
  57. {
  58. return *((int *)this);
  59. }
  60. inline int r() const { return _color[0]; }
  61. inline int g() const { return _color[1]; }
  62. inline int b() const { return _color[2]; }
  63. inline int a() const { return _color[3]; }
  64. unsigned char &operator[](int index)
  65. {
  66. return _color[index];
  67. }
  68. const unsigned char &operator[](int index) const
  69. {
  70. return _color[index];
  71. }
  72. bool operator == (const Color &rhs) const
  73. {
  74. return ( *((int *)this) == *((int *)&rhs) );
  75. }
  76. bool operator != (const Color &rhs) const
  77. {
  78. return !(operator==(rhs));
  79. }
  80. Color &operator=( const Color &rhs )
  81. {
  82. SetRawColor( rhs.GetRawColor() );
  83. return *this;
  84. }
  85. Color &operator=( const color32 &rhs )
  86. {
  87. _color[0] = rhs.r;
  88. _color[1] = rhs.g;
  89. _color[2] = rhs.b;
  90. _color[3] = rhs.a;
  91. return *this;
  92. }
  93. color32 ToColor32() const
  94. {
  95. color32 newColor;
  96. newColor.r = _color[0];
  97. newColor.g = _color[1];
  98. newColor.b = _color[2];
  99. newColor.a = _color[3];
  100. return newColor;
  101. }
  102. private:
  103. unsigned char _color[4];
  104. };
  105. #endif // COLOR_H