Color.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "Color.h"
  24. using namespace dsr;
  25. ColorRgbI32 ColorRgbI32::saturate() const {
  26. int32_t red = this->red;
  27. int32_t green = this->green;
  28. int32_t blue = this->blue;
  29. if (red < 0) { red = 0; }
  30. if (red > 255) { red = 255; }
  31. if (green < 0) { green = 0; }
  32. if (green > 255) { green = 255; }
  33. if (blue < 0) { blue = 0; }
  34. if (blue > 255) { blue = 255; }
  35. return ColorRgbI32(red, green, blue);
  36. }
  37. ColorRgbI32 ColorRgbI32::mix(const ColorRgbI32& colorA, const ColorRgbI32& colorB, float weight) {
  38. float invWeight = 1.0f - weight;
  39. return (colorA * invWeight) + (colorB * weight);
  40. }
  41. ColorRgbI32::ColorRgbI32(const ReadableString &content) : red(0), green(0), blue(0) {
  42. List<ReadableString> elements = string_split(content, U',');
  43. int givenChannels = elements.length();
  44. if (givenChannels >= 1) {
  45. this-> red = string_parseInteger(elements[0]);
  46. if (givenChannels >= 2) {
  47. this-> green = string_parseInteger(elements[1]);
  48. if (givenChannels >= 3) {
  49. this-> blue = string_parseInteger(elements[2]);
  50. }
  51. }
  52. }
  53. }
  54. ColorRgbaI32 ColorRgbaI32::saturate() const {
  55. int32_t red = this->red;
  56. int32_t green = this->green;
  57. int32_t blue = this->blue;
  58. int32_t alpha = this->alpha;
  59. if (red < 0) { red = 0; }
  60. if (red > 255) { red = 255; }
  61. if (green < 0) { green = 0; }
  62. if (green > 255) { green = 255; }
  63. if (blue < 0) { blue = 0; }
  64. if (blue > 255) { blue = 255; }
  65. if (alpha < 0) { alpha = 0; }
  66. if (alpha > 255) { alpha = 255; }
  67. return ColorRgbaI32(red, green, blue, alpha);
  68. }
  69. ColorRgbaI32 ColorRgbaI32::mix(const ColorRgbaI32& colorA, const ColorRgbaI32& colorB, float weight) {
  70. float invWeight = 1.0f - weight;
  71. return (colorA * invWeight) + (colorB * weight);
  72. }
  73. ColorRgbaI32::ColorRgbaI32(const ReadableString &content) : red(0), green(0), blue(0), alpha(255) {
  74. List<ReadableString> elements = string_split(content, U',');
  75. int givenChannels = elements.length();
  76. if (givenChannels >= 1) {
  77. this-> red = string_parseInteger(elements[0]);
  78. if (givenChannels >= 2) {
  79. this-> green = string_parseInteger(elements[1]);
  80. if (givenChannels >= 3) {
  81. this-> blue = string_parseInteger(elements[2]);
  82. if (givenChannels >= 4) {
  83. this-> alpha = string_parseInteger(elements[3]);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. String& dsr::string_toStreamIndented(String& target, const ColorRgbI32& source, const ReadableString& indentation) {
  90. string_append(target, indentation, source.red, U",", source.green, U",", source.blue);
  91. return target;
  92. }
  93. String& dsr::string_toStreamIndented(String& target, const ColorRgbaI32& source, const ReadableString& indentation) {
  94. string_append(target, indentation, source.red, U",", source.green, U",", source.blue, U",", source.alpha);
  95. return target;
  96. }
  97. String& dsr::string_toStreamIndented(String& target, const Color4xU8& source, const ReadableString& indentation) {
  98. string_append(target, indentation, source.channels[0], U",", source.channels[1], U",", source.channels[2], U",", source.channels[3]);
  99. return target;
  100. }