Team Fortress 2 Source Code as on 22/4/2020
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.

103 lines
2.0 KiB

  1. //========= Copyright 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. //-----------------------------------------------------------------------------
  13. // Purpose: Basic handler for an rgb set of colors
  14. // This class is fully inline
  15. //-----------------------------------------------------------------------------
  16. class Color
  17. {
  18. public:
  19. // constructors
  20. Color()
  21. {
  22. *((int *)this) = 0;
  23. }
  24. Color(int _r,int _g,int _b)
  25. {
  26. SetColor(_r, _g, _b, 0);
  27. }
  28. Color(int _r,int _g,int _b,int _a)
  29. {
  30. SetColor(_r, _g, _b, _a);
  31. }
  32. // set the color
  33. // r - red component (0-255)
  34. // g - green component (0-255)
  35. // b - blue component (0-255)
  36. // a - alpha component, controls transparency (0 - transparent, 255 - opaque);
  37. void SetColor(int _r, int _g, int _b, int _a = 0)
  38. {
  39. _color[0] = (unsigned char)_r;
  40. _color[1] = (unsigned char)_g;
  41. _color[2] = (unsigned char)_b;
  42. _color[3] = (unsigned char)_a;
  43. }
  44. void GetColor(int &_r, int &_g, int &_b, int &_a) const
  45. {
  46. _r = _color[0];
  47. _g = _color[1];
  48. _b = _color[2];
  49. _a = _color[3];
  50. }
  51. void SetRawColor( int color32 )
  52. {
  53. *((int *)this) = color32;
  54. }
  55. int GetRawColor() const
  56. {
  57. return *((int *)this);
  58. }
  59. inline int r() const { return _color[0]; }
  60. inline int g() const { return _color[1]; }
  61. inline int b() const { return _color[2]; }
  62. inline int a() const { return _color[3]; }
  63. unsigned char &operator[](int index)
  64. {
  65. return _color[index];
  66. }
  67. const unsigned char &operator[](int index) const
  68. {
  69. return _color[index];
  70. }
  71. bool operator == (const Color &rhs) const
  72. {
  73. return ( *((int *)this) == *((int *)&rhs) );
  74. }
  75. bool operator != (const Color &rhs) const
  76. {
  77. return !(operator==(rhs));
  78. }
  79. Color &operator=( const Color &rhs )
  80. {
  81. SetRawColor( rhs.GetRawColor() );
  82. return *this;
  83. }
  84. private:
  85. unsigned char _color[4];
  86. };
  87. #endif // COLOR_H