Color.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP;
  25. /**
  26. * Simple RGB color container
  27. * @since 25.0.0
  28. */
  29. class Color {
  30. private int $r;
  31. private int $g;
  32. private int $b;
  33. /**
  34. * @since 25.0.0
  35. */
  36. public function __construct($r, $g, $b) {
  37. $this->r = $r;
  38. $this->g = $g;
  39. $this->b = $b;
  40. }
  41. /**
  42. * Returns the red color component of this color as an int from 0 to 255
  43. *
  44. * @since 25.0.0
  45. */
  46. public function red(): int {
  47. return $this->r;
  48. }
  49. /**
  50. * Returns the red color component of this color as a float from 0 to 1
  51. *
  52. * @since 25.0.0
  53. */
  54. public function redF(): float {
  55. return $this->r / 255;
  56. }
  57. /**
  58. * Returns the green color component of this color as an int from 0 to 255
  59. *
  60. * @since 25.0.0
  61. */
  62. public function green(): int {
  63. return $this->g;
  64. }
  65. /**
  66. * Returns the green color component of this color as a float from 0 to 1
  67. *
  68. * @since 25.0.0
  69. */
  70. public function greenF(): float {
  71. return $this->g / 255;
  72. }
  73. /**
  74. * Returns the green blue component of this color as an int from 0 to 255
  75. *
  76. * @since 25.0.0
  77. */
  78. public function blue(): int {
  79. return $this->b;
  80. }
  81. /**
  82. * Returns the blue color component of this color as a float from 0 to 1
  83. *
  84. * @since 25.0.0
  85. */
  86. public function blueF(): float {
  87. return $this->g / 255;
  88. }
  89. /**
  90. * Returns the name of the color in the format "#RRGGBB"; i.e. a "#" character followed by three two-digit hexadecimal numbers.
  91. *
  92. * @since 25.0.0
  93. */
  94. public function name(): string {
  95. return sprintf("#%02x%02x%02x", $this->r, $this->g, $this->b);
  96. }
  97. /**
  98. * Mix two colors
  99. *
  100. * @param int $steps the number of intermediate colors that should be generated for the palette
  101. * @param Color $color1 the first color
  102. * @param Color $color2 the second color
  103. * @return list<Color>
  104. * @since 25.0.0
  105. */
  106. public static function mixPalette(int $steps, Color $color1, Color $color2): array {
  107. $palette = [$color1];
  108. $step = self::stepCalc($steps, [$color1, $color2]);
  109. for ($i = 1; $i < $steps; $i++) {
  110. $r = intval($color1->red() + ($step[0] * $i));
  111. $g = intval($color1->green() + ($step[1] * $i));
  112. $b = intval($color1->blue() + ($step[2] * $i));
  113. $palette[] = new Color($r, $g, $b);
  114. }
  115. return $palette;
  116. }
  117. /**
  118. * Alpha blend another color with a given opacity to this color
  119. *
  120. * @return Color The new color
  121. * @since 25.0.0
  122. */
  123. public function alphaBlending(float $opacity, Color $source): Color {
  124. return new Color(
  125. (int)((1 - $opacity) * $source->red() + $opacity * $this->red()),
  126. (int)((1 - $opacity) * $source->green() + $opacity * $this->green()),
  127. (int)((1 - $opacity) * $source->blue() + $opacity * $this->blue())
  128. );
  129. }
  130. /**
  131. * Calculate steps between two Colors
  132. * @param int $steps start color
  133. * @param Color[] $ends end color
  134. * @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
  135. * @since 25.0.0
  136. */
  137. private static function stepCalc(int $steps, array $ends): array {
  138. $step = [];
  139. $step[0] = ($ends[1]->red() - $ends[0]->red()) / $steps;
  140. $step[1] = ($ends[1]->green() - $ends[0]->green()) / $steps;
  141. $step[2] = ($ends[1]->blue() - $ends[0]->blue()) / $steps;
  142. return $step;
  143. }
  144. }