ColorCMY.php 2.1 KB
Newer Older
targoo's avatar
WIP D8  
targoo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
<?php

/**
 * @file
 * Contains Drupal\color_field\ColorCMY.
 */

namespace Drupal\color_field;

/**
 * ColorCMY represents the CMY color format.
 */
class ColorCMY extends ColorBase {

  /**
   * The cyan
   * @var float
   */
  private $cyan;

  /**
   * The magenta
   * @var float
   */
  private $magenta;

  /**
   * The yellow
   * @var float
   */
  private $yellow;

  /**
   * Create a new CMYK color
   *
   * @param float $cyan
   *  The cyan
   * @param float $magenta
   *  The magenta
   * @param float $yellow
   *  The yellow
   * @param float $opacity
   *  The opacity
   */
  public function __construct($cyan, $magenta, $yellow, $opacity) {
    $this->cyan = $cyan;
    $this->magenta = $magenta;
    $this->yellow = $yellow;
    $this->opacity = floatval($opacity);
  }

  /**
   * Get the amount of Cyan
   *
   * @return int The amount of cyan
   */
  public function getCyan() {
    return $this->cyan;
  }

  /**
   * Get the amount of Magenta
   *
   * @return int The amount of magenta
   */
  public function getMagenta() {
    return $this->magenta;
  }

  /**
   * Get the amount of Yellow
   *
   * @return int The amount of yellow
   */
  public function getYellow() {
    return $this->yellow;
  }

  /**
   * A string representation of this color in the current format
   *
   * @param bool $opacity
   *   Whether or not to display the opacity.
   *
   * @return string
   *   The color in format: #RRGGBB
   */
  public function toString($opacity = TRUE) {
  }

  /**
   * {@inheritdoc}
   */
  public function toHex() {
    return $this->toRGB()->toHex();
  }

  /**
   * {@inheritdoc}
   */
  public function toRGB() {
    $red = (1 - $this->cyan) * 255;
    $green = (1 - $this->magenta) * 255;
    $blue = (1 - $this->yellow) * 255;
    return new ColorRGB($red, $green, $blue, $this->getOpacity());
  }

  /**
   * {@inheritdoc}
   */
  public function toCMY() {
    $cyan = ($this->cyan * (1 - $this->key) + $this->key);
    $magenta = ($this->magenta * (1 - $this->key) + $this->key);
    $yellow = ($this->yellow * (1 - $this->key) + $this->key);
    return new ColorCMY($cyan, $magenta, $yellow);
  }

}