ColorCMY.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Drupal\color_field;
  3. /**
  4. * ColorCMY represents the CMY color format.
  5. */
  6. class ColorCMY extends ColorBase {
  7. /**
  8. * The cyan
  9. * @var float
  10. */
  11. private $cyan;
  12. /**
  13. * The magenta
  14. * @var float
  15. */
  16. private $magenta;
  17. /**
  18. * The yellow
  19. * @var float
  20. */
  21. private $yellow;
  22. /**
  23. * Create a new CMYK color
  24. *
  25. * @param float $cyan
  26. * The cyan
  27. * @param float $magenta
  28. * The magenta
  29. * @param float $yellow
  30. * The yellow
  31. * @param float $opacity
  32. * The opacity
  33. */
  34. public function __construct($cyan, $magenta, $yellow, $opacity) {
  35. $this->cyan = $cyan;
  36. $this->magenta = $magenta;
  37. $this->yellow = $yellow;
  38. $this->opacity = floatval($opacity);
  39. }
  40. /**
  41. * Get the amount of Cyan
  42. *
  43. * @return int The amount of cyan
  44. */
  45. public function getCyan() {
  46. return $this->cyan;
  47. }
  48. /**
  49. * Get the amount of Magenta
  50. *
  51. * @return int The amount of magenta
  52. */
  53. public function getMagenta() {
  54. return $this->magenta;
  55. }
  56. /**
  57. * Get the amount of Yellow
  58. *
  59. * @return int The amount of yellow
  60. */
  61. public function getYellow() {
  62. return $this->yellow;
  63. }
  64. /**
  65. * A string representation of this color in the current format
  66. *
  67. * @param bool $opacity
  68. * Whether or not to display the opacity.
  69. *
  70. * @return string
  71. * The color in format: #RRGGBB
  72. */
  73. public function toString($opacity = TRUE) {
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function toHex() {
  79. return $this->toRGB()->toHex();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function toRGB() {
  85. $red = (1 - $this->cyan) * 255;
  86. $green = (1 - $this->magenta) * 255;
  87. $blue = (1 - $this->yellow) * 255;
  88. return new ColorRGB($red, $green, $blue, $this->getOpacity());
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function toCMY() {
  94. $cyan = ($this->cyan * (1 - $this->key) + $this->key);
  95. $magenta = ($this->magenta * (1 - $this->key) + $this->key);
  96. $yellow = ($this->yellow * (1 - $this->key) + $this->key);
  97. return new ColorCMY($cyan, $magenta, $yellow);
  98. }
  99. }