Color.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <stdint.h>
  26. #include "../api/stringAPI.h"
  27. namespace dsr {
  28. // RGB color with 32 bits per channel
  29. // Values outside of the 0..255 byte range may cause unexpected behaviour
  30. struct ColorRgbI32 {
  31. int32_t red, green, blue;
  32. ColorRgbI32() : red(0), green(0), blue(0) {}
  33. explicit ColorRgbI32(int32_t uniform) : red(uniform), green(uniform), blue(uniform) {}
  34. ColorRgbI32(int32_t red, int32_t green, int32_t blue) : red(red), green(green), blue(blue) {}
  35. // Clamp to the valid range
  36. ColorRgbI32 saturate() const;
  37. static ColorRgbI32 mix(const ColorRgbI32& colorA, const ColorRgbI32& colorB, float weight);
  38. // Create a color from a string
  39. explicit ColorRgbI32(const ReadableString &content);
  40. };
  41. inline ColorRgbI32 operator*(const ColorRgbI32& left, float right) {
  42. return ColorRgbI32((float)left.red * right, (float)left.green * right, (float)left.blue * right);
  43. }
  44. inline ColorRgbI32 operator*(const ColorRgbI32& left, int32_t right) {
  45. return ColorRgbI32(left.red * right, left.green * right, left.blue * right);
  46. }
  47. inline ColorRgbI32 operator+(const ColorRgbI32& left, const ColorRgbI32& right) {
  48. return ColorRgbI32(left.red + right.red, left.green + right.green, left.blue + right.blue);
  49. }
  50. inline bool operator== (const ColorRgbI32& a, const ColorRgbI32& b) {
  51. return a.red == b.red && a.green == b.green && a.blue == b.blue;
  52. }
  53. inline bool operator!= (const ColorRgbI32& a, const ColorRgbI32& b) {
  54. return !(a == b);
  55. }
  56. // RGBA color with 32 bits per channel
  57. // Values outside of the 0..255 byte range may cause unexpected behaviour
  58. struct ColorRgbaI32 {
  59. int32_t red, green, blue, alpha;
  60. ColorRgbaI32() : red(0), green(0), blue(0), alpha(0) {}
  61. ColorRgbaI32(ColorRgbI32 rgb, int32_t alpha) : red(rgb.red), green(rgb.green), blue(rgb.blue), alpha(alpha) {}
  62. explicit ColorRgbaI32(int32_t uniform) : red(uniform), green(uniform), blue(uniform), alpha(uniform) {}
  63. ColorRgbaI32(int32_t red, int32_t green, int32_t blue, int32_t alpha) : red(red), green(green), blue(blue), alpha(alpha) {}
  64. // Clamp to the valid range
  65. ColorRgbaI32 saturate() const;
  66. static ColorRgbaI32 mix(const ColorRgbaI32& colorA, const ColorRgbaI32& colorB, float weight);
  67. // Create a color from a string
  68. explicit ColorRgbaI32(const ReadableString &content);
  69. };
  70. inline ColorRgbaI32 operator*(const ColorRgbaI32& left, float right) {
  71. return ColorRgbaI32((float)left.red * right, (float)left.green * right, (float)left.blue * right, (float)left.alpha * right);
  72. }
  73. inline ColorRgbaI32 operator*(const ColorRgbaI32& left, int32_t right) {
  74. return ColorRgbaI32(left.red * right, left.green * right, left.blue * right, left.alpha * right);
  75. }
  76. inline ColorRgbaI32 operator+(const ColorRgbaI32& left, const ColorRgbaI32& right) {
  77. return ColorRgbaI32(left.red + right.red, left.green + right.green, left.blue + right.blue, left.alpha + right.alpha);
  78. }
  79. inline bool operator== (const ColorRgbaI32& a, const ColorRgbaI32& b) {
  80. return a.red == b.red && a.green == b.green && a.blue == b.blue && a.alpha == b.alpha;
  81. }
  82. inline bool operator!= (const ColorRgbaI32& a, const ColorRgbaI32& b) {
  83. return !(a == b);
  84. }
  85. // TODO: Can this type be hidden from the external API?
  86. // RGBA color in arbitrary pack order for speed
  87. // Use ImageRgbaU8Impl::packRgba to construct for a specific pack order
  88. union Color4xU8 {
  89. uint32_t packed;
  90. uint8_t channels[4];
  91. Color4xU8() : packed(0) {}
  92. explicit Color4xU8(uint32_t packed) : packed(packed) {}
  93. Color4xU8(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) : channels{first, second, third, fourth} {}
  94. bool isUniformByte() {
  95. int first = this->channels[0];
  96. return this->channels[1] == first && this->channels[2] == first && this->channels[3] == first;
  97. }
  98. };
  99. inline bool operator== (const Color4xU8& a, const Color4xU8& b) {
  100. return a.packed == b.packed;
  101. }
  102. inline bool operator!= (const Color4xU8& a, const Color4xU8& b) {
  103. return !(a == b);
  104. }
  105. // Serialization
  106. String& string_toStreamIndented(String& target, const ColorRgbI32& source, const ReadableString& indentation);
  107. String& string_toStreamIndented(String& target, const ColorRgbaI32& source, const ReadableString& indentation);
  108. String& string_toStreamIndented(String& target, const Color4xU8& source, const ReadableString& indentation);
  109. }
  110. #endif