color.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*************************************************************************/
  2. /* color.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef COLOR_H
  30. #define COLOR_H
  31. #include "ustring.h"
  32. /**
  33. @author Juan Linietsky <[email protected]>
  34. */
  35. struct Color {
  36. union {
  37. struct {
  38. float r;
  39. float g;
  40. float b;
  41. float a;
  42. };
  43. float components[4];
  44. };
  45. bool operator==(const Color &p_color) const { return (r==p_color.r && g==p_color.g && b==p_color.b && a==p_color.a ); }
  46. bool operator!=(const Color &p_color) const { return (r!=p_color.r || g!=p_color.g || b!=p_color.b || a!=p_color.a ); }
  47. uint32_t to_32() const;
  48. uint32_t to_ARGB32() const;
  49. float gray() const;
  50. float get_h() const;
  51. float get_s() const;
  52. float get_v() const;
  53. void set_hsv(float p_h, float p_s, float p_v, float p_alpha=1.0);
  54. _FORCE_INLINE_ float& operator[](int idx) {
  55. return components[idx];
  56. }
  57. _FORCE_INLINE_ const float& operator[](int idx) const {
  58. return components[idx];
  59. }
  60. void invert();
  61. void contrast();
  62. Color inverted() const;
  63. Color contrasted() const;
  64. _FORCE_INLINE_ Color linear_interpolate(const Color& p_b, float p_t) const {
  65. Color res=*this;
  66. res.r+= (p_t * (p_b.r-r));
  67. res.g+= (p_t * (p_b.g-g));
  68. res.b+= (p_t * (p_b.b-b));
  69. res.a+= (p_t * (p_b.a-a));
  70. return res;
  71. }
  72. _FORCE_INLINE_ Color blend(const Color& p_over) const {
  73. Color res;
  74. float sa = 1.0 - p_over.a;
  75. res.a = a*sa+p_over.a;
  76. if (res.a==0) {
  77. return Color(0,0,0,0);
  78. } else {
  79. res.r = (r*a*sa + p_over.r * p_over.a)/res.a;
  80. res.g = (g*a*sa + p_over.g * p_over.a)/res.a;
  81. res.b = (b*a*sa + p_over.b * p_over.a)/res.a;
  82. }
  83. return res;
  84. }
  85. static Color hex(uint32_t p_hex);
  86. static Color html(const String& p_color);
  87. static bool html_is_valid(const String& p_color);
  88. String to_html(bool p_alpha=true) const;
  89. _FORCE_INLINE_ bool operator<(const Color& p_color) const; //used in set keys
  90. operator String() const;
  91. /**
  92. * No construct parameters, r=0, g=0, b=0. a=255
  93. */
  94. _FORCE_INLINE_ Color() {
  95. r=0; g=0; b=0; a=1.0;
  96. }
  97. /**
  98. * RGB / RGBA construct parameters. Alpha is optional, but defaults to 1.0
  99. */
  100. _FORCE_INLINE_ Color(float p_r,float p_g,float p_b,float p_a=1.0) { r=p_r; g=p_g; b=p_b; a=p_a; }
  101. };
  102. bool Color::operator<(const Color& p_color) const {
  103. if (r==p_color.r) {
  104. if (g==p_color.g) {
  105. if(b==p_color.b) {
  106. return (a<p_color.a);
  107. } else
  108. return (b<p_color.b);
  109. } else
  110. return g<p_color.g;
  111. } else
  112. return r<p_color.r;
  113. }
  114. #endif