Color.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCP;
  7. /**
  8. * Simple RGB color container
  9. * @since 25.0.0
  10. */
  11. class Color {
  12. private int $r;
  13. private int $g;
  14. private int $b;
  15. /**
  16. * @since 25.0.0
  17. */
  18. public function __construct($r, $g, $b) {
  19. $this->r = $r;
  20. $this->g = $g;
  21. $this->b = $b;
  22. }
  23. /**
  24. * Returns the red color component of this color as an int from 0 to 255
  25. *
  26. * @since 25.0.0
  27. */
  28. public function red(): int {
  29. return $this->r;
  30. }
  31. /**
  32. * Returns the red color component of this color as a float from 0 to 1
  33. *
  34. * @since 25.0.0
  35. */
  36. public function redF(): float {
  37. return $this->r / 255;
  38. }
  39. /**
  40. * Returns the green color component of this color as an int from 0 to 255
  41. *
  42. * @since 25.0.0
  43. */
  44. public function green(): int {
  45. return $this->g;
  46. }
  47. /**
  48. * Returns the green color component of this color as a float from 0 to 1
  49. *
  50. * @since 25.0.0
  51. */
  52. public function greenF(): float {
  53. return $this->g / 255;
  54. }
  55. /**
  56. * Returns the green blue component of this color as an int from 0 to 255
  57. *
  58. * @since 25.0.0
  59. */
  60. public function blue(): int {
  61. return $this->b;
  62. }
  63. /**
  64. * Returns the blue color component of this color as a float from 0 to 1
  65. *
  66. * @since 25.0.0
  67. */
  68. public function blueF(): float {
  69. return $this->g / 255;
  70. }
  71. /**
  72. * Returns the name of the color in the format "#RRGGBB"; i.e. a "#" character followed by three two-digit hexadecimal numbers.
  73. *
  74. * @since 25.0.0
  75. */
  76. public function name(): string {
  77. return sprintf("#%02x%02x%02x", $this->r, $this->g, $this->b);
  78. }
  79. /**
  80. * Mix two colors
  81. *
  82. * @param int $steps the number of intermediate colors that should be generated for the palette
  83. * @param Color $color1 the first color
  84. * @param Color $color2 the second color
  85. * @return list<Color>
  86. * @since 25.0.0
  87. */
  88. public static function mixPalette(int $steps, Color $color1, Color $color2): array {
  89. $palette = [$color1];
  90. $step = self::stepCalc($steps, [$color1, $color2]);
  91. for ($i = 1; $i < $steps; $i++) {
  92. $r = intval($color1->red() + ($step[0] * $i));
  93. $g = intval($color1->green() + ($step[1] * $i));
  94. $b = intval($color1->blue() + ($step[2] * $i));
  95. $palette[] = new Color($r, $g, $b);
  96. }
  97. return $palette;
  98. }
  99. /**
  100. * Alpha blend another color with a given opacity to this color
  101. *
  102. * @return Color The new color
  103. * @since 25.0.0
  104. */
  105. public function alphaBlending(float $opacity, Color $source): Color {
  106. return new Color(
  107. (int)((1 - $opacity) * $source->red() + $opacity * $this->red()),
  108. (int)((1 - $opacity) * $source->green() + $opacity * $this->green()),
  109. (int)((1 - $opacity) * $source->blue() + $opacity * $this->blue())
  110. );
  111. }
  112. /**
  113. * Calculate steps between two Colors
  114. * @param int $steps start color
  115. * @param Color[] $ends end color
  116. * @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
  117. * @since 25.0.0
  118. */
  119. private static function stepCalc(int $steps, array $ends): array {
  120. $step = [];
  121. $step[0] = ($ends[1]->red() - $ends[0]->red()) / $steps;
  122. $step[1] = ($ends[1]->green() - $ends[0]->green()) / $steps;
  123. $step[2] = ($ends[1]->blue() - $ends[0]->blue()) / $steps;
  124. return $step;
  125. }
  126. }