Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. /*-------------------------------------------------------------------------
  2. RgbColor provides a color object that can be directly consumed by NeoPixelBus
  3.  
  4. Written by Michael C. Miller.
  5.  
  6. I invest time and resources providing this open source code,
  7. please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
  8.  
  9. -------------------------------------------------------------------------
  10. This file is part of the Makuna/NeoPixelBus library.
  11.  
  12. NeoPixelBus is free software: you can redistribute it and/or modify
  13. it under the terms of the GNU Lesser General Public License as
  14. published by the Free Software Foundation, either version 3 of
  15. the License, or (at your option) any later version.
  16.  
  17. NeoPixelBus is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU Lesser General Public License for more details.
  21.  
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with NeoPixel. If not, see
  24. <http://www.gnu.org/licenses/>.
  25. -------------------------------------------------------------------------*/
  26. #pragma once
  27.  
  28. #include <Arduino.h>
  29.  
  30. struct HslColor;
  31. struct HsbColor;
  32. struct HtmlColor;
  33.  
  34. // ------------------------------------------------------------------------
  35. // RgbColor represents a color object that is represented by Red, Green, Blue
  36. // component values. It contains helpful color routines to manipulate the
  37. // color.
  38. // ------------------------------------------------------------------------
  39. struct RgbColor
  40. {
  41. // ------------------------------------------------------------------------
  42. // Construct a RgbColor using R, G, B values (0-255)
  43. // ------------------------------------------------------------------------
  44. RgbColor(uint8_t r, uint8_t g, uint8_t b) :
  45. R(r), G(g), B(b)
  46. {
  47. };
  48.  
  49. // ------------------------------------------------------------------------
  50. // Construct a RgbColor using a single brightness value (0-255)
  51. // This works well for creating gray tone colors
  52. // (0) = black, (255) = white, (128) = gray
  53. // ------------------------------------------------------------------------
  54. RgbColor(uint8_t brightness) :
  55. R(brightness), G(brightness), B(brightness)
  56. {
  57. };
  58.  
  59. // ------------------------------------------------------------------------
  60. // Construct a RgbColor using HtmlColor
  61. // ------------------------------------------------------------------------
  62. RgbColor(const HtmlColor& color);
  63.  
  64. // ------------------------------------------------------------------------
  65. // Construct a RgbColor using HslColor
  66. // ------------------------------------------------------------------------
  67. RgbColor(const HslColor& color);
  68.  
  69. // ------------------------------------------------------------------------
  70. // Construct a RgbColor using HsbColor
  71. // ------------------------------------------------------------------------
  72. RgbColor(const HsbColor& color);
  73.  
  74. // ------------------------------------------------------------------------
  75. // Construct a RgbColor that will have its values set in latter operations
  76. // CAUTION: The R,G,B members are not initialized and may not be consistent
  77. // ------------------------------------------------------------------------
  78. RgbColor()
  79. {
  80. };
  81.  
  82. // ------------------------------------------------------------------------
  83. // Comparison operators
  84. // ------------------------------------------------------------------------
  85. bool operator==(const RgbColor& other) const
  86. {
  87. return (R == other.R && G == other.G && B == other.B);
  88. };
  89.  
  90. bool operator!=(const RgbColor& other) const
  91. {
  92. return !(*this == other);
  93. };
  94.  
  95. // ------------------------------------------------------------------------
  96. // CalculateBrightness will calculate the overall brightness
  97. // NOTE: This is a simple linear brightness
  98. // ------------------------------------------------------------------------
  99. uint8_t CalculateBrightness() const;
  100.  
  101. // ------------------------------------------------------------------------
  102. // Darken will adjust the color by the given delta toward black
  103. // NOTE: This is a simple linear change
  104. // delta - (0-255) the amount to dim the color
  105. // ------------------------------------------------------------------------
  106. void Darken(uint8_t delta);
  107.  
  108. // ------------------------------------------------------------------------
  109. // Lighten will adjust the color by the given delta toward white
  110. // NOTE: This is a simple linear change
  111. // delta - (0-255) the amount to lighten the color
  112. // ------------------------------------------------------------------------
  113. void Lighten(uint8_t delta);
  114.  
  115. // ------------------------------------------------------------------------
  116. // LinearBlend between two colors by the amount defined by progress variable
  117. // left - the color to start the blend at
  118. // right - the color to end the blend at
  119. // progress - (0.0 - 1.0) value where 0 will return left and 1.0 will return right
  120. // and a value between will blend the color weighted linearly between them
  121. // ------------------------------------------------------------------------
  122. static RgbColor LinearBlend(const RgbColor& left, const RgbColor& right, float progress);
  123.  
  124. // ------------------------------------------------------------------------
  125. // BilinearBlend between four colors by the amount defined by 2d variable
  126. // c00 - upper left quadrant color
  127. // c01 - upper right quadrant color
  128. // c10 - lower left quadrant color
  129. // c11 - lower right quadrant color
  130. // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space
  131. // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space
  132. // ------------------------------------------------------------------------
  133. static RgbColor BilinearBlend(const RgbColor& c00,
  134. const RgbColor& c01,
  135. const RgbColor& c10,
  136. const RgbColor& c11,
  137. float x,
  138. float y);
  139.  
  140. // ------------------------------------------------------------------------
  141. // Red, Green, Blue color members (0-255) where
  142. // (0,0,0) is black and (255,255,255) is white
  143. // ------------------------------------------------------------------------
  144. uint8_t R;
  145. uint8_t G;
  146. uint8_t B;
  147. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement