Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 *
19
 * @package    theme_universe
20
 * @copyright  2023 Marcin Czaja
21
 * @author     Marcin Czaja
22
 *               {@link https://rosea.io.co}
23
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
24
 */
25
 
26
namespace theme_universe;
27
 
28
/**
29
 * The theme's toolbox.
30
 */
31
class toolbox {
32
 
33
 
34
    /**
35
     * hex2rgb
36
     *
37
     * @param  mixed $hex
38
     *
39
     * @return void
40
     */
41
    public static function hex2rgb($hex = '#000000') {
42
        $f = function ($x) {
43
            return hexdec($x);
44
        };
45
 
46
        return array_map($f, str_split(str_replace("#", "", $hex), 2));
47
    }
48
 
49
    /**
50
     * rgb2hex
51
     *
52
     * @param  mixed $rgb
53
     *
54
     * @return void
55
     */
56
    public static function rgb2hex($rgb = array(0, 0, 0)) {
57
        $f = function ($x) {
58
            return str_pad(dechex($x), 2, "0", STR_PAD_LEFT);
59
        };
60
 
61
        return "#" . implode("", array_map($f, $rgb));
62
    }
63
 
64
    /**
65
     * mix
66
     *
67
     * @param  mixed $color_1
68
     * @param  mixed $color_2
69
     * @param  mixed $weight
70
     *
71
     * @return void
72
     */
73
    public static function mix($color_1 = array(0, 0, 0), $color_2 = array(0, 0, 0), $weight = 0.5) {
74
        $f = function ($x) use ($weight) {
75
            return $weight * $x;
76
        };
77
 
78
        $g = function ($x) use ($weight) {
79
            return (1 - $weight) * $x;
80
        };
81
 
82
        $h = function ($x, $y) {
83
            return round($x + $y);
84
        };
85
 
86
        return array_map($h, array_map($f, $color_1), array_map($g, $color_2));
87
    }
88
 
89
    /**
90
     * tint
91
     *
92
     * @param  mixed $color
93
     * @param  mixed $weight
94
     *
95
     * @return void
96
     */
97
    public static function tint($color, $weight = 0.5) {
98
        $t = $color;
99
 
100
        if (is_string($color)) {
101
            $t = \theme_universe\toolbox::hex2rgb($color);
102
        }
103
 
104
        $u = \theme_universe\toolbox::mix($t, array(255, 255, 255), $weight);
105
 
106
        if (is_string($color)) {
107
            return \theme_universe\toolbox::rgb2hex($u);
108
        }
109
 
110
        return $u;
111
    }
112
 
113
    /**
114
     * tone
115
     *
116
     * @param  mixed $color
117
     * @param  mixed $weight
118
     *
119
     * @return void
120
     */
121
    public static function tone($color, $weight = 0.5) {
122
        $t = $color;
123
 
124
        if (is_string($color)) {
125
            $t = \theme_universe\toolbox::hex2rgb($color);
126
        }
127
 
128
        $u = \theme_universe\toolbox::mix($t, array(128, 128, 128), $weight);
129
 
130
        if (is_string($color)) {
131
            return \theme_universe\toolbox::rgb2hex($u);
132
        }
133
 
134
        return $u;
135
    }
136
 
137
    /**
138
     * shade
139
     *
140
     * @param  mixed $color
141
     * @param  mixed $weight
142
     *
143
     * @return void
144
     */
145
    public static function shade($color, $weight = 0.5) {
146
        $t = $color;
147
 
148
        if (is_string($color)) {
149
            $t = \theme_universe\toolbox::hex2rgb($color);
150
        }
151
 
152
        $u = \theme_universe\toolbox::mix($t, array(0, 0, 0), $weight);
153
 
154
        if (is_string($color)) {
155
            return \theme_universe\toolbox::rgb2hex($u);
156
        }
157
 
158
        return $u;
159
    }
160
}