summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Util/Color.php
blob: cf88f41795e00310fa5777bc21375bbd9b702944 (plain)
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
119
120
121
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Util;

/**
 * Provide functions to change and convert colors.
 */
class Color
{
    /**
     * Convert a given color string to an rgb-array containing
     * each color as a decimal value.
     *
     * @param $color    The color-string #RRGGBB
     *
     * @return array    The converted rgb-array.
     */
    public static function rgbAsArray($color)
    {
        if (substr($color, 0, 1) !== '#') {
            $color = '#' . $color;
        }
        if (strlen($color) !== 7) {
            return;
        }
        $r = (float)intval(substr($color, 1, 2), 16);
        $g = (float)intval(substr($color, 3, 2), 16);
        $b = (float)intval(substr($color, 5, 2), 16);
        return array($r, $g, $b);
    }

    /**
     * Convert a rgb array to a color-string
     *
     * @param array $rgb    The rgb-array
     *
     * @return string   The color string #RRGGBB
     */
    public static function arrayToRgb(array $rgb)
    {
        $r = (string)dechex($rgb[0]);
        $g = (string)dechex($rgb[1]);
        $b = (string)dechex($rgb[2]);
        return '#'
            . (strlen($r) > 1 ? $r : '0' . $r)
            . (strlen($g) > 1 ? $g : '0' . $g)
            . (strlen($b) > 1 ? $b : '0' . $b);
    }

    /**
     * Change the saturation for a given color.
     *
     * @param $color    string  The color to change
     * @param $change   float   The change.
     *                   0.0 creates a black-and-white image.
     *                   0.5 reduces the color saturation by half.
     *                   1.0 causes no change.
     *                   2.0 doubles the color saturation.
     * @return string
     */
    public static function changeSaturation($color, $change)
    {
        return self::arrayToRgb(self::changeRgbSaturation(self::rgbAsArray($color), $change));
    }

    /**
     * Change the brightness for a given color
     *
     * @param $color    string  The color to change
     * @param $change   float   The change in percent
     *
     * @return string
     */
    public static function changeBrightness($color, $change)
    {
        return self::arrayToRgb(self::changeRgbBrightness(self::rgbAsArray($color), $change));
    }

    /**
     * @param $rgb      array   The rgb-array to change
     * @param $change   float   The factor
     *
     * @return array    The updated rgb-array
     */
    private static function changeRgbSaturation(array $rgb, $change)
    {
        $pr = 0.499; // 0.299
        $pg = 0.387; // 0.587
        $pb = 0.114; // 0.114
        $r = $rgb[0];
        $g = $rgb[1];
        $b = $rgb[2];
        $p = sqrt(
            $r * $r * $pr +
            $g * $g * $pg +
            $b * $b * $pb
        );
        $rgb[0] = (int)($p + ($r - $p) * $change);
        $rgb[1] = (int)($p + ($g - $p) * $change);
        $rgb[2] = (int)($p + ($b - $p) * $change);
        return $rgb;
    }

    /**
     * @param $rgb      array   The rgb-array to change
     * @param $change   float   The factor
     *
     * @return array    The updated rgb-array
     */
    private static function changeRgbBrightness(array $rgb, $change)
    {
        $red = $rgb[0] + ($rgb[0] * $change);
        $green = $rgb[1] + ($rgb[1] * $change);
        $blue = $rgb[2] + ($rgb[2] * $change);
        $rgb[0] = $red < 255 ? (int) $red : 255;
        $rgb[1] = $green < 255 ? (int) $green : 255;
        $rgb[2] = $blue < 255 ? (int) $blue : 255;
        return $rgb;
    }
}