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 * Gray color.
16 */
17class Gray implements ColorInterface
18{
19    /**
20     * Gray value.
21     *
22     * @var integer
23     */
24    protected $gray;
25
26    /**
27     * Creates a new gray color.
28     *
29     * A low gray value means black, while a high value means white.
30     *
31     * @param integer $gray
32     */
33    public function __construct($gray)
34    {
35        if ($gray < 0 || $gray > 100) {
36            throw new Exception\InvalidArgumentException('Gray must be between 0 and 100');
37        }
38
39        $this->gray = (int) $gray;
40    }
41
42    /**
43     * Returns the gray value.
44     *
45     * @return integer
46     */
47    public function getGray()
48    {
49        return $this->gray;
50    }
51
52    /**
53     * toRgb(): defined by ColorInterface.
54     *
55     * @see    ColorInterface::toRgb()
56     * @return Rgb
57     */
58    public function toRgb()
59    {
60        return new Rgb($this->gray * 2.55, $this->gray * 2.55, $this->gray * 2.55);
61    }
62
63    /**
64     * toCmyk(): defined by ColorInterface.
65     *
66     * @see    ColorInterface::toCmyk()
67     * @return Cmyk
68     */
69    public function toCmyk()
70    {
71        return new Cmyk(0, 0, 0, 100 - $this->gray);
72    }
73
74    /**
75     * toGray(): defined by ColorInterface.
76     *
77     * @see    ColorInterface::toGray()
78     * @return Gray
79     */
80    public function toGray()
81    {
82        return $this;
83    }
84}