Color.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_IMAGE_COLOR
  24. #define DFPSR_IMAGE_COLOR
  25. #include <cstdint>
  26. #include "../../api/stringAPI.h"
  27. namespace dsr {
  28. struct ColorRgbI32;
  29. struct ColorRgbaI32;
  30. inline ColorRgbI32 operator * (const ColorRgbI32& left, float right);
  31. inline ColorRgbI32 operator * (const ColorRgbI32& left, int32_t right);
  32. inline ColorRgbI32 operator + (const ColorRgbI32& left, const ColorRgbI32& right);
  33. inline bool operator == (const ColorRgbI32& a, const ColorRgbI32& b);
  34. inline bool operator != (const ColorRgbI32& a, const ColorRgbI32& b);
  35. inline ColorRgbaI32 operator *( const ColorRgbaI32& left, float right);
  36. inline ColorRgbaI32 operator * (const ColorRgbaI32& left, int32_t right);
  37. inline ColorRgbaI32 operator + (const ColorRgbaI32& left, const ColorRgbaI32& right);
  38. inline bool operator == (const ColorRgbaI32& a, const ColorRgbaI32& b);
  39. inline bool operator != (const ColorRgbaI32& a, const ColorRgbaI32& b);
  40. // RGB color with 32 bits per channel
  41. // Values outside of the 0..255 byte range may cause unexpected behaviour
  42. struct ColorRgbI32 {
  43. int32_t red, green, blue;
  44. ColorRgbI32() : red(0), green(0), blue(0) {}
  45. explicit ColorRgbI32(int32_t uniform) : red(uniform), green(uniform), blue(uniform) {}
  46. ColorRgbI32(int32_t red, int32_t green, int32_t blue) : red(red), green(green), blue(blue) {}
  47. // Get the color clamped to the visible range.
  48. ColorRgbI32 saturate() const {
  49. int32_t red = this->red;
  50. int32_t green = this->green;
  51. int32_t blue = this->blue;
  52. if (red < 0) { red = 0; }
  53. if (red > 255) { red = 255; }
  54. if (green < 0) { green = 0; }
  55. if (green > 255) { green = 255; }
  56. if (blue < 0) { blue = 0; }
  57. if (blue > 255) { blue = 255; }
  58. return ColorRgbI32(red, green, blue);
  59. }
  60. static ColorRgbI32 mix(const ColorRgbI32& colorA, const ColorRgbI32& colorB, float weight) {
  61. float invWeight = 1.0f - weight;
  62. return (colorA * invWeight) + (colorB * weight);
  63. }
  64. // Create a color from a string.
  65. explicit ColorRgbI32(const ReadableString &content) : red(0), green(0), blue(0) {
  66. int givenChannels = 0;
  67. string_split_callback([this, &givenChannels](ReadableString channelValue) {
  68. if (givenChannels == 0) {
  69. this->red = string_toInteger(channelValue);
  70. } else if (givenChannels == 1) {
  71. this->green = string_toInteger(channelValue);
  72. } else if (givenChannels == 2) {
  73. this->blue = string_toInteger(channelValue);
  74. }
  75. givenChannels++;
  76. }, content, U',');
  77. }
  78. };
  79. inline ColorRgbI32 operator * (const ColorRgbI32& left, float right) {
  80. return ColorRgbI32((float)left.red * right, (float)left.green * right, (float)left.blue * right);
  81. }
  82. inline ColorRgbI32 operator * (const ColorRgbI32& left, int32_t right) {
  83. return ColorRgbI32(left.red * right, left.green * right, left.blue * right);
  84. }
  85. inline ColorRgbI32 operator + (const ColorRgbI32& left, const ColorRgbI32& right) {
  86. return ColorRgbI32(left.red + right.red, left.green + right.green, left.blue + right.blue);
  87. }
  88. inline bool operator == (const ColorRgbI32& a, const ColorRgbI32& b) {
  89. return a.red == b.red && a.green == b.green && a.blue == b.blue;
  90. }
  91. inline bool operator != (const ColorRgbI32& a, const ColorRgbI32& b) {
  92. return !(a == b);
  93. }
  94. // RGBA color with 32 bits per channel
  95. // Values outside of the 0..255 byte range may cause unexpected behaviour
  96. struct ColorRgbaI32 {
  97. int32_t red, green, blue, alpha;
  98. ColorRgbaI32() : red(0), green(0), blue(0), alpha(0) {}
  99. ColorRgbaI32(ColorRgbI32 rgb, int32_t alpha) : red(rgb.red), green(rgb.green), blue(rgb.blue), alpha(alpha) {}
  100. explicit ColorRgbaI32(int32_t uniform) : red(uniform), green(uniform), blue(uniform), alpha(uniform) {}
  101. ColorRgbaI32(int32_t red, int32_t green, int32_t blue, int32_t alpha) : red(red), green(green), blue(blue), alpha(alpha) {}
  102. // Get the color clamped to the visible range.
  103. ColorRgbaI32 saturate() const {
  104. int32_t red = this->red;
  105. int32_t green = this->green;
  106. int32_t blue = this->blue;
  107. int32_t alpha = this->alpha;
  108. if (red < 0) { red = 0; }
  109. if (red > 255) { red = 255; }
  110. if (green < 0) { green = 0; }
  111. if (green > 255) { green = 255; }
  112. if (blue < 0) { blue = 0; }
  113. if (blue > 255) { blue = 255; }
  114. if (alpha < 0) { alpha = 0; }
  115. if (alpha > 255) { alpha = 255; }
  116. return ColorRgbaI32(red, green, blue, alpha);
  117. }
  118. static ColorRgbaI32 mix(const ColorRgbaI32& colorA, const ColorRgbaI32& colorB, float weight) {
  119. float invWeight = 1.0f - weight;
  120. return (colorA * invWeight) + (colorB * weight);
  121. }
  122. // Create a color from a string.
  123. explicit ColorRgbaI32(const ReadableString &content) : red(0), green(0), blue(0), alpha(255) {
  124. int givenChannels = 0;
  125. string_split_callback([this, &givenChannels](ReadableString channelValue) {
  126. if (givenChannels == 0) {
  127. this->red = string_toInteger(channelValue);
  128. } else if (givenChannels == 1) {
  129. this->green = string_toInteger(channelValue);
  130. } else if (givenChannels == 2) {
  131. this->blue = string_toInteger(channelValue);
  132. } else if (givenChannels == 3) {
  133. this->alpha = string_toInteger(channelValue);
  134. }
  135. givenChannels++;
  136. }, content, U',');
  137. }
  138. };
  139. inline ColorRgbaI32 operator *( const ColorRgbaI32& left, float right) {
  140. return ColorRgbaI32((float)left.red * right, (float)left.green * right, (float)left.blue * right, (float)left.alpha * right);
  141. }
  142. inline ColorRgbaI32 operator * (const ColorRgbaI32& left, int32_t right) {
  143. return ColorRgbaI32(left.red * right, left.green * right, left.blue * right, left.alpha * right);
  144. }
  145. inline ColorRgbaI32 operator + (const ColorRgbaI32& left, const ColorRgbaI32& right) {
  146. return ColorRgbaI32(left.red + right.red, left.green + right.green, left.blue + right.blue, left.alpha + right.alpha);
  147. }
  148. inline bool operator == (const ColorRgbaI32& a, const ColorRgbaI32& b) {
  149. return a.red == b.red && a.green == b.green && a.blue == b.blue && a.alpha == b.alpha;
  150. }
  151. inline bool operator != (const ColorRgbaI32& a, const ColorRgbaI32& b) {
  152. return !(a == b);
  153. }
  154. // Serialization
  155. inline String& string_toStreamIndented(String& target, const ColorRgbI32& source, const ReadableString& indentation) {
  156. string_append(target, indentation, source.red, U",", source.green, U",", source.blue);
  157. return target;
  158. }
  159. inline String& string_toStreamIndented(String& target, const ColorRgbaI32& source, const ReadableString& indentation) {
  160. string_append(target, indentation, source.red, U",", source.green, U",", source.blue, U",", source.alpha);
  161. return target;
  162. }
  163. }
  164. #endif