1<?php
2/**
3 * BaconQrCode
4 *
5 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
6 * @copyright 2013 Ben 'DASPRiD' Scholzen
7 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8 */
9
10namespace BaconQrCode\Renderer\Color;
11
12use BaconQrCode\Exception;
13
14/**
15 * CMYK color.
16 */
17class Cmyk implements ColorInterface
18{
19    /**
20     * Cyan value.
21     *
22     * @var integer
23     */
24    protected $cyan;
25
26    /**
27     * Magenta value.
28     *
29     * @var integer
30     */
31    protected $magenta;
32
33    /**
34     * Yellow value.
35     *
36     * @var integer
37     */
38    protected $yellow;
39
40    /**
41     * Black value.
42     *
43     * @var integer
44     */
45    protected $black;
46
47    /**
48     * Creates a new CMYK color.
49     *
50     * @param integer $cyan
51     * @param integer $magenta
52     * @param integer $yellow
53     * @param integer $black
54     */
55    public function __construct($cyan, $magenta, $yellow, $black)
56    {
57        if ($cyan < 0 || $cyan > 100) {
58            throw new Exception\InvalidArgumentException('Cyan must be between 0 and 100');
59        }
60
61        if ($magenta < 0 || $magenta > 100) {
62            throw new Exception\InvalidArgumentException('Magenta must be between 0 and 100');
63        }
64
65        if ($yellow < 0 || $yellow > 100) {
66            throw new Exception\InvalidArgumentException('Yellow must be between 0 and 100');
67        }
68
69        if ($black < 0 || $black > 100) {
70            throw new Exception\InvalidArgumentException('Black must be between 0 and 100');
71        }
72
73        $this->cyan    = (int) $cyan;
74        $this->magenta = (int) $magenta;
75        $this->yellow  = (int) $yellow;
76        $this->black   = (int) $black;
77    }
78
79    /**
80     * Returns the cyan value.
81     *
82     * @return integer
83     */
84    public function getCyan()
85    {
86        return $this->cyan;
87    }
88
89    /**
90     * Returns the magenta value.
91     *
92     * @return integer
93     */
94    public function getMagenta()
95    {
96        return $this->magenta;
97    }
98
99    /**
100     * Returns the yellow value.
101     *
102     * @return integer
103     */
104    public function getYellow()
105    {
106        return $this->yellow;
107    }
108
109    /**
110     * Returns the black value.
111     *
112     * @return integer
113     */
114    public function getBlack()
115    {
116        return $this->black;
117    }
118
119    /**
120     * toRgb(): defined by ColorInterface.
121     *
122     * @see    ColorInterface::toRgb()
123     * @return Rgb
124     */
125    public function toRgb()
126    {
127        $k = $this->black / 100;
128        $c = (-$k * $this->cyan + $k * 100 + $this->cyan) / 100;
129        $m = (-$k * $this->magenta + $k * 100 + $this->magenta) / 100;
130        $y = (-$k * $this->yellow + $k * 100 + $this->yellow) / 100;
131
132        return new Rgb(
133            -$c * 255 + 255,
134            -$m * 255 + 255,
135            -$y * 255 + 255
136        );
137    }
138
139    /**
140     * toCmyk(): defined by ColorInterface.
141     *
142     * @see    ColorInterface::toCmyk()
143     * @return Cmyk
144     */
145    public function toCmyk()
146    {
147        return $this;
148    }
149
150    /**
151     * toGray(): defined by ColorInterface.
152     *
153     * @see    ColorInterface::toGray()
154     * @return Gray
155     */
156    public function toGray()
157    {
158        return $this->toRgb()->toGray();
159    }
160}