bij-elkaar-passende-kleuren-generator
Gesponsorde koppelingen
PHP script bestanden
Voorbeeld (een van de vele mogelijkheden):
De functies:Code (php)
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
122
123
124
125
126
127
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
122
123
124
125
126
127
<?php
/**
* This function calculates the precision of a float or integer. In other words, the number of digits in a number.
*
* @param int|float $number
* @return int
*/
function getPrecision($number) {
if (!is_numeric($number)) {
throw new InvalidArgumentException('Argument $number must be a number');
}
settype($number, 'string');
if ($dotPosition = strrpos($number, '.')) {
$number = substr($number, 0, $dotPosition) . substr($number, $dotPosition + 1);
}
$number = ltrim($number, '0');
if ($exponentPosition = strrpos($number, 'E')) {
$number = substr($number, 0, $exponentPosition);
}
return strlen($number);
}
/**
* This function creates a range of at each other appropriate colors with as many contrast as possible in RGB format.
*
* @uses hsbToRgb()
* @uses getPrecision()
* @uses significantRound()
* @param int $number The number of colors that needs to be created
* @param int|float|null $hue This parameter can be a int or float between 0 and 360. If you pass null through, the hue is used as a variable factor by calculating the colors.
* @param int|float|null $saturation This parameter can be a int or float between 0 and 100. If you pass null through, the saturation is used as a variable factor by calculating the colors.
* @param unknown_type $brightness This parameter can be a int or float between 0 and 100. If you pass null through, the brightness is used as a variable factor by calculating the colors.
* @param int|float $hueStart This parameter can be a int or float between 0 and 360. It specifies the starting point of the hue. This parameter has only effect when parameter $hue is set to null. It can be bigger then $hueStop.
* @param int|float $hueStop This parameter can be a int or float between 0 and 360. It specifies the ending point of the hue. This parameter has only effect when parameter $hue is set to null. It can be smaller then $hueStart.
* @param int|float $saturationStart This parameter can be a int or float between 0 and 100. It specifies the starting point of the saturation. This parameter has only effect when parameter $saturation is set to null. It can be bigger then $saturationStop.
* @param int|float $saturationStop This parameter can be a int or float between 0 and 100. It specifies the ending point of the saturation. This parameter has only effect when parameter $saturation is set to null. It can be smaller then $saturationStart.
* @param int|float $brightnessStart This parameter can be a int or float between 0 and 100. It specifies the starting point of the brightness. This parameter has only effect when parameter $brightness is set to null. It can be bigger then $brightnessStop.
* @param int|float $brightnessStop This parameter can be a int or float between 0 and 100. It specifies the ending point of the brightness. This parameter has only effect when parameter $brightness is set to null. It can be smaller then $brightnessStart.
* @param boolean $hueReverse When this parameter is set to true, the calculating of the hue value is reversed. This parameter has only effect when parameter $hue is set to null.
* @param boolean $saturationReverse When this parameter is set to true, the calculating of the saturation value is reversed. This parameter has only effect when parameter $saturation is set to null.
* @param boolean $brightnessReverse When this parameter is set to true, the calculating of the brightness value is reversed. This parameter has only effect when parameter $brightness is set to null.
* @return array The return value is a multidimesional array. Each top-level element has a array with elements 'red', 'green' and 'blue', holding integers from 0 to 255.
*/
function getColors(
$number,
$hue,
$saturation,
$brightness,
$hueStart = 0,
$hueStop = 360,
$saturationStart = 0,
$saturationStop = 100,
$brightnessStart = 0,
$brightnessStop = 100,
$hueReverse = false,
$saturationReverse = false,
$brightnessReverse = false
) {
$hueStart = significantRound($hueStart, getPrecision($hueStart));
if ($hueStart < 0 || $hueStart > 360) {
throw new LengthException('Argument $hueStart is not a number between 0 and 360.');
}
$hueStop = significantRound($hueStop, getPrecision($hueStop));
if ($hueStop < 0 || $hueStop > 360) {
throw new LengthException('Argument $hueStop is not a number between 0 and 360.');
}
$saturationStart = significantRound($saturationStart, getPrecision($saturationStart));
if ($saturationStart < 0 || $saturationStart > 100) {
throw new LengthException('Argument $saturaionStart is not a number between 0 and 100.');
}
$saturationStop = significantRound($saturationStop, getPrecision($saturationStop));
if ($saturationStop < 0 || $saturationStop > 100) {
throw new LengthException('Argument $saturaionStop is not a number between 0 and 100.');
}
$brightnessStart = significantRound($brightnessStart, getPrecision($brightnessStart));
if ($brightnessStart < 0 || $brightnessStart > 100) {
throw new LengthException('Argument $brightnessStart is not a number between 0 and 100.');
}
$brightnessStop = significantRound($brightnessStop, getPrecision($brightnessStop));
if ($brightnessStop < 0 || $brightnessStop > 100) {
throw new LengthException('Argument $brightnessStop is not a number between 0 and 100.');
}
if (!is_bool($hueReverse)) {
throw new InvalidArgumentException('Argument $hueReverse is not a boolean.');
}
if (!is_bool($saturationReverse)) {
throw new InvalidArgumentException('Argument $saturationReverse is not a boolean.');
}
if (!is_bool($brightnessReverse)) {
throw new InvalidArgumentException('Argument $brightnessReverse is not a boolean.');
}
$hueRange = max($hueStart, $hueStop) == $hueStop ? $hueStop - $hueStart : 360 - $hueStart + $hueStop;
$saturationRange = max($saturationStart, $saturationStop) == $saturationStop ? $saturationStop - $saturationStart : 100 - $saturationStart + $saturationStop;
$brightnessRange = max($brightnessStart, $brightnessStop) == $brightnessStop ? $brightnessStop - $brightnessStart : 100 - $brightnessStart + $brightnessStop;
$currentHue = $hue;
$currentSaturation = $saturation;
$currentBrightness = $brightness;
$hueBetween = 0;
$saturationBetween = 0;
$brightnessBetween = 0;
if (is_null($hue)) {
$hueBetween = $hueRange / $number;
$currentHue = $hueReverse ? $hueStop : $hueStart;
}
if (is_null($saturation)) {
$saturationBetween = $saturationRange / ($number - 1);
$currentSaturation = $saturationReverse ? $saturationStop : $saturationStart;
}
if (is_null($brightness)) {
$brightnessBetween = $brightnessRange / ($number - 1);
$currentBrightness = $brightnessReverse ? $brightnessStop : $brightnessStart;
}
$colors = array();
for ($i = 0; $i < $number; $i++) {
$colors[] = hsbToRgb($currentHue, $currentSaturation, $currentBrightness);
$currentHue += $hueReverse ? -$hueBetween : $hueBetween;
$currentSaturation += $saturationReverse ? -$saturationBetween : $saturationBetween;
$currentBrightness += $brightnessReverse ? -$brightnessBetween : $brightnessBetween;
$currentHue = significantRound($currentHue, 3) > 360 ? 0 : $currentHue;
$currentSaturation = significantRound($currentSaturation, 3) > 100 ? 0 : $currentSaturation;
$currentBrightness = significantRound($currentBrightness, 3) > 100 ? 0 : $currentBrightness;
$currentHue = significantRound($currentHue, 3) < 0 ? 360 : $currentHue;
$currentSaturation = significantRound($currentSaturation, 3) < 0 ? 100 : $currentSaturation;
$currentBrightness = significantRound($currentBrightness, 3) < 0 ? 100 : $currentBrightness;
}
return $colors;
}
?>
/**
* This function calculates the precision of a float or integer. In other words, the number of digits in a number.
*
* @param int|float $number
* @return int
*/
function getPrecision($number) {
if (!is_numeric($number)) {
throw new InvalidArgumentException('Argument $number must be a number');
}
settype($number, 'string');
if ($dotPosition = strrpos($number, '.')) {
$number = substr($number, 0, $dotPosition) . substr($number, $dotPosition + 1);
}
$number = ltrim($number, '0');
if ($exponentPosition = strrpos($number, 'E')) {
$number = substr($number, 0, $exponentPosition);
}
return strlen($number);
}
/**
* This function creates a range of at each other appropriate colors with as many contrast as possible in RGB format.
*
* @uses hsbToRgb()
* @uses getPrecision()
* @uses significantRound()
* @param int $number The number of colors that needs to be created
* @param int|float|null $hue This parameter can be a int or float between 0 and 360. If you pass null through, the hue is used as a variable factor by calculating the colors.
* @param int|float|null $saturation This parameter can be a int or float between 0 and 100. If you pass null through, the saturation is used as a variable factor by calculating the colors.
* @param unknown_type $brightness This parameter can be a int or float between 0 and 100. If you pass null through, the brightness is used as a variable factor by calculating the colors.
* @param int|float $hueStart This parameter can be a int or float between 0 and 360. It specifies the starting point of the hue. This parameter has only effect when parameter $hue is set to null. It can be bigger then $hueStop.
* @param int|float $hueStop This parameter can be a int or float between 0 and 360. It specifies the ending point of the hue. This parameter has only effect when parameter $hue is set to null. It can be smaller then $hueStart.
* @param int|float $saturationStart This parameter can be a int or float between 0 and 100. It specifies the starting point of the saturation. This parameter has only effect when parameter $saturation is set to null. It can be bigger then $saturationStop.
* @param int|float $saturationStop This parameter can be a int or float between 0 and 100. It specifies the ending point of the saturation. This parameter has only effect when parameter $saturation is set to null. It can be smaller then $saturationStart.
* @param int|float $brightnessStart This parameter can be a int or float between 0 and 100. It specifies the starting point of the brightness. This parameter has only effect when parameter $brightness is set to null. It can be bigger then $brightnessStop.
* @param int|float $brightnessStop This parameter can be a int or float between 0 and 100. It specifies the ending point of the brightness. This parameter has only effect when parameter $brightness is set to null. It can be smaller then $brightnessStart.
* @param boolean $hueReverse When this parameter is set to true, the calculating of the hue value is reversed. This parameter has only effect when parameter $hue is set to null.
* @param boolean $saturationReverse When this parameter is set to true, the calculating of the saturation value is reversed. This parameter has only effect when parameter $saturation is set to null.
* @param boolean $brightnessReverse When this parameter is set to true, the calculating of the brightness value is reversed. This parameter has only effect when parameter $brightness is set to null.
* @return array The return value is a multidimesional array. Each top-level element has a array with elements 'red', 'green' and 'blue', holding integers from 0 to 255.
*/
function getColors(
$number,
$hue,
$saturation,
$brightness,
$hueStart = 0,
$hueStop = 360,
$saturationStart = 0,
$saturationStop = 100,
$brightnessStart = 0,
$brightnessStop = 100,
$hueReverse = false,
$saturationReverse = false,
$brightnessReverse = false
) {
$hueStart = significantRound($hueStart, getPrecision($hueStart));
if ($hueStart < 0 || $hueStart > 360) {
throw new LengthException('Argument $hueStart is not a number between 0 and 360.');
}
$hueStop = significantRound($hueStop, getPrecision($hueStop));
if ($hueStop < 0 || $hueStop > 360) {
throw new LengthException('Argument $hueStop is not a number between 0 and 360.');
}
$saturationStart = significantRound($saturationStart, getPrecision($saturationStart));
if ($saturationStart < 0 || $saturationStart > 100) {
throw new LengthException('Argument $saturaionStart is not a number between 0 and 100.');
}
$saturationStop = significantRound($saturationStop, getPrecision($saturationStop));
if ($saturationStop < 0 || $saturationStop > 100) {
throw new LengthException('Argument $saturaionStop is not a number between 0 and 100.');
}
$brightnessStart = significantRound($brightnessStart, getPrecision($brightnessStart));
if ($brightnessStart < 0 || $brightnessStart > 100) {
throw new LengthException('Argument $brightnessStart is not a number between 0 and 100.');
}
$brightnessStop = significantRound($brightnessStop, getPrecision($brightnessStop));
if ($brightnessStop < 0 || $brightnessStop > 100) {
throw new LengthException('Argument $brightnessStop is not a number between 0 and 100.');
}
if (!is_bool($hueReverse)) {
throw new InvalidArgumentException('Argument $hueReverse is not a boolean.');
}
if (!is_bool($saturationReverse)) {
throw new InvalidArgumentException('Argument $saturationReverse is not a boolean.');
}
if (!is_bool($brightnessReverse)) {
throw new InvalidArgumentException('Argument $brightnessReverse is not a boolean.');
}
$hueRange = max($hueStart, $hueStop) == $hueStop ? $hueStop - $hueStart : 360 - $hueStart + $hueStop;
$saturationRange = max($saturationStart, $saturationStop) == $saturationStop ? $saturationStop - $saturationStart : 100 - $saturationStart + $saturationStop;
$brightnessRange = max($brightnessStart, $brightnessStop) == $brightnessStop ? $brightnessStop - $brightnessStart : 100 - $brightnessStart + $brightnessStop;
$currentHue = $hue;
$currentSaturation = $saturation;
$currentBrightness = $brightness;
$hueBetween = 0;
$saturationBetween = 0;
$brightnessBetween = 0;
if (is_null($hue)) {
$hueBetween = $hueRange / $number;
$currentHue = $hueReverse ? $hueStop : $hueStart;
}
if (is_null($saturation)) {
$saturationBetween = $saturationRange / ($number - 1);
$currentSaturation = $saturationReverse ? $saturationStop : $saturationStart;
}
if (is_null($brightness)) {
$brightnessBetween = $brightnessRange / ($number - 1);
$currentBrightness = $brightnessReverse ? $brightnessStop : $brightnessStart;
}
$colors = array();
for ($i = 0; $i < $number; $i++) {
$colors[] = hsbToRgb($currentHue, $currentSaturation, $currentBrightness);
$currentHue += $hueReverse ? -$hueBetween : $hueBetween;
$currentSaturation += $saturationReverse ? -$saturationBetween : $saturationBetween;
$currentBrightness += $brightnessReverse ? -$brightnessBetween : $brightnessBetween;
$currentHue = significantRound($currentHue, 3) > 360 ? 0 : $currentHue;
$currentSaturation = significantRound($currentSaturation, 3) > 100 ? 0 : $currentSaturation;
$currentBrightness = significantRound($currentBrightness, 3) > 100 ? 0 : $currentBrightness;
$currentHue = significantRound($currentHue, 3) < 0 ? 360 : $currentHue;
$currentSaturation = significantRound($currentSaturation, 3) < 0 ? 100 : $currentSaturation;
$currentBrightness = significantRound($currentBrightness, 3) < 0 ? 100 : $currentBrightness;
}
return $colors;
}
?>