1<?php
2/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
3
4namespace Icinga\Util;
5
6/**
7 * Provide functions to change and convert colors.
8 */
9class Color
10{
11    /**
12     * Convert a given color string to an rgb-array containing
13     * each color as a decimal value.
14     *
15     * @param $color    The color-string #RRGGBB
16     *
17     * @return array    The converted rgb-array.
18     */
19    public static function rgbAsArray($color)
20    {
21        if (substr($color, 0, 1) !== '#') {
22            $color = '#' . $color;
23        }
24        if (strlen($color) !== 7) {
25            return;
26        }
27        $r = (float)intval(substr($color, 1, 2), 16);
28        $g = (float)intval(substr($color, 3, 2), 16);
29        $b = (float)intval(substr($color, 5, 2), 16);
30        return array($r, $g, $b);
31    }
32
33    /**
34     * Convert a rgb array to a color-string
35     *
36     * @param array $rgb    The rgb-array
37     *
38     * @return string   The color string #RRGGBB
39     */
40    public static function arrayToRgb(array $rgb)
41    {
42        $r = (string)dechex($rgb[0]);
43        $g = (string)dechex($rgb[1]);
44        $b = (string)dechex($rgb[2]);
45        return '#'
46            . (strlen($r) > 1 ? $r : '0' . $r)
47            . (strlen($g) > 1 ? $g : '0' . $g)
48            . (strlen($b) > 1 ? $b : '0' . $b);
49    }
50
51    /**
52     * Change the saturation for a given color.
53     *
54     * @param $color    string  The color to change
55     * @param $change   float   The change.
56     *                   0.0 creates a black-and-white image.
57     *                   0.5 reduces the color saturation by half.
58     *                   1.0 causes no change.
59     *                   2.0 doubles the color saturation.
60     * @return string
61     */
62    public static function changeSaturation($color, $change)
63    {
64        return self::arrayToRgb(self::changeRgbSaturation(self::rgbAsArray($color), $change));
65    }
66
67    /**
68     * Change the brightness for a given color
69     *
70     * @param $color    string  The color to change
71     * @param $change   float   The change in percent
72     *
73     * @return string
74     */
75    public static function changeBrightness($color, $change)
76    {
77        return self::arrayToRgb(self::changeRgbBrightness(self::rgbAsArray($color), $change));
78    }
79
80    /**
81     * @param $rgb      array   The rgb-array to change
82     * @param $change   float   The factor
83     *
84     * @return array    The updated rgb-array
85     */
86    private static function changeRgbSaturation(array $rgb, $change)
87    {
88        $pr = 0.499; // 0.299
89        $pg = 0.387; // 0.587
90        $pb = 0.114; // 0.114
91        $r = $rgb[0];
92        $g = $rgb[1];
93        $b = $rgb[2];
94        $p = sqrt(
95            $r * $r * $pr +
96            $g * $g * $pg +
97            $b * $b * $pb
98        );
99        $rgb[0] = (int)($p + ($r - $p) * $change);
100        $rgb[1] = (int)($p + ($g - $p) * $change);
101        $rgb[2] = (int)($p + ($b - $p) * $change);
102        return $rgb;
103    }
104
105    /**
106     * @param $rgb      array   The rgb-array to change
107     * @param $change   float   The factor
108     *
109     * @return array    The updated rgb-array
110     */
111    private static function changeRgbBrightness(array $rgb, $change)
112    {
113        $red = $rgb[0] + ($rgb[0] * $change);
114        $green = $rgb[1] + ($rgb[1] * $change);
115        $blue = $rgb[2] + ($rgb[2] * $change);
116        $rgb[0] = $red < 255 ? (int) $red : 255;
117        $rgb[1] = $green < 255 ? (int) $green : 255;
118        $rgb[2] = $blue < 255 ? (int) $blue : 255;
119        return $rgb;
120    }
121}
122