Completed
Push — master ( 0ee2e7...b858c3 )
by Nicholas
06:23
created

css::rgb()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 4
nc 2
nop 4
crap 12
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) {
0 ignored issues
show
Bug introduced by
The property alpha does not seem to exist in projectcleverweb\color\color.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable $c does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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