aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ColourValue.php
blob: 486de4567951b09d04ff87ff23c65b14e1075061 (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
<?php

/**
 * ColorValue.php
 * model class for table color_values
 *
 * Objects of this class holds a colour's name (its purpose)
 * and the value for the colour.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2018-2019
 * @since       4.5
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 *
 * @property string $id alias column for colour_id
 * @property string $colour_id database column
 * @property I18NString $description database column
 * @property string $value database column
 * @property int $mkdate database column
 * @property int $chdate database column
 */
class ColourValue extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'colour_values';

        $config['i18n_fields']['description'] = true;

        parent::configure($config);
    }

    /**
     * $colours is an array with all colour values that is filled
     * when a colour is requested.
     */
    protected static $colours;

    /**
     * The find method is overloaded here since the table is usually very
     * small and the colour values are requested often. They are stored
     * in an array and served from there to save database requests.
     */
    public static function find($id)
    {
        if (!is_array(self::$colours)) {
            self::$colours = [];
            //Load all colours:
            $colours = self::findBySql('TRUE');
            foreach ($colours as $colour) {
                self::$colours[$colour->id] = $colour;
            }
        }

        return self::$colours[$id];
    }


    /**
     * DEVELOPER WARNING: Do not rename this method to setValue since setValue
     * is a SimpleORMap reserved method for setting attribute values
     * of a SORM object!
     */
    public function setColourValue($r = 0xff, $g = 0xff, $b = 0xff, $a = 0xff)
    {
        $value = dechex($r) . dechex($g) . dechex($b) . dechex($a);
        $this->value = $value;
    }


    public function __toString()
    {
        $r = $this->value[0] . $this->value[1];
        $g = $this->value[2] . $this->value[3];
        $b = $this->value[4] . $this->value[5];

        //The color values are output as '#RRGGBB'.
        //This way it is compatible with the input type color.
        return mb_strtolower('#' . $r . $g . $b);
    }


    public function toRGBAFunction()
    {
        $r = $this->value[0] . $this->value[1];
        $g = $this->value[2] . $this->value[3];
        $b = $this->value[4] . $this->value[5];
        $a = $this->value[6] . $this->value[7];

        return sprintf(
            'rgba(%s %s %s %s)',
            hexdec($r),
            hexdec($g),
            hexdec($b),
            hexdec($a)
        );
    }
}