Color.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. int givenChannels = 0;
  43. string_split_callback([this, &givenChannels](ReadableString channelValue) {
  44. if (givenChannels == 0) {
  45. this->red = string_toInteger(channelValue);
  46. } else if (givenChannels == 1) {
  47. this->green = string_toInteger(channelValue);
  48. } else if (givenChannels == 2) {
  49. this->blue = string_toInteger(channelValue);
  50. }
  51. givenChannels++;
  52. }, content, U',');
  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. int givenChannels = 0;
  75. string_split_callback([this, &givenChannels](ReadableString channelValue) {
  76. if (givenChannels == 0) {
  77. this->red = string_toInteger(channelValue);
  78. } else if (givenChannels == 1) {
  79. this->green = string_toInteger(channelValue);
  80. } else if (givenChannels == 2) {
  81. this->blue = string_toInteger(channelValue);
  82. } else if (givenChannels == 3) {
  83. this->alpha = string_toInteger(channelValue);
  84. }
  85. givenChannels++;
  86. }, content, U',');
  87. }
  88. String& dsr::string_toStreamIndented(String& target, const ColorRgbI32& source, const ReadableString& indentation) {
  89. string_append(target, indentation, source.red, U",", source.green, U",", source.blue);
  90. return target;
  91. }
  92. String& dsr::string_toStreamIndented(String& target, const ColorRgbaI32& source, const ReadableString& indentation) {
  93. string_append(target, indentation, source.red, U",", source.green, U",", source.blue, U",", source.alpha);
  94. return target;
  95. }
  96. String& dsr::string_toStreamIndented(String& target, const Color4xU8& source, const ReadableString& indentation) {
  97. string_append(target, indentation, source.channels[0], U",", source.channels[1], U",", source.channels[2], U",", source.channels[3]);
  98. return target;
  99. }