1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace projectcleverweb\color; |
4
|
|
|
|
5
|
|
|
class css { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Force alpha value to be present where possible. |
9
|
|
|
* @var boolean |
10
|
|
|
*/ |
11
|
|
|
public static $force_alpha = FALSE; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Choose the best way to represent the color as a CSS value. Will use either |
15
|
|
|
* a hex or rgba value depending on the alpha value. |
16
|
|
|
* |
17
|
|
|
* @param color|array $color The color |
18
|
|
|
* @return string The CSS value |
19
|
|
|
*/ |
20
|
|
|
public static function best($color) { |
21
|
|
|
if (!(is_object($color) && is_a($color, __CLASS__))) { |
22
|
|
|
$color = new color($color); |
23
|
|
|
} |
24
|
|
|
if ($color->alpha == 1.0 && !static::$force_alpha) { |
|
|
|
|
25
|
|
|
return static::hex($color->rgb['r'], $color->rgb['g'], $color->rgb['b']); |
26
|
|
|
} |
27
|
|
|
return static::rgb($color->rgb['r'], $color->rgb['g'], $color->rgb['b'], $c->alpha); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Convert and RGB value to a CSS hex string |
32
|
|
|
* |
33
|
|
|
* @param float $r The red value |
34
|
|
|
* @param float $g The green value |
35
|
|
|
* @param float $b The blue value |
36
|
|
|
* @return string The CSS string |
37
|
|
|
*/ |
38
|
|
|
public static function hex(float $r, float $g, float $b) { |
39
|
|
|
return '#'.generate::rgb_to_hex($r, $g, $b); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Convert and RGB value to a CSS rgb or rgba string |
44
|
|
|
* |
45
|
|
|
* @param float $r The red value |
46
|
|
|
* @param float $g The green value |
47
|
|
|
* @param float $b The blue value |
48
|
|
|
* @return string The CSS string |
49
|
|
|
*/ |
50
|
|
|
public static function rgb(float $r, float $g, float $b, float $a = 1.0) { |
51
|
|
|
if ($a == 1.0 && !static::$force_alpha) { |
52
|
|
|
return sprintf('rgb(%s,%s,%s)', $r, $g, $b); |
53
|
|
|
} |
54
|
|
|
return sprintf('rgba(%s,%s,%s,%s)', $r, $g, $b, $a); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert and HSL value to a CSS hsl or hsla string |
59
|
|
|
* |
60
|
|
|
* @param float $h The hue value |
61
|
|
|
* @param float $s The saturation value |
62
|
|
|
* @param float $l The light value |
63
|
|
|
* @return string The CSS string |
64
|
|
|
*/ |
65
|
|
|
public static function hsl(float $h, float $s, float $l, float $a = 1.0) { |
66
|
|
|
if ($a == 1.0 && !static::$force_alpha) { |
67
|
|
|
return sprintf('hsl(%s,%s,%s)', $h, $s, $l); |
68
|
|
|
} |
69
|
|
|
return sprintf('hsla(%s,%s,%s,%s)', $h, $s, $l, $a); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.