forked from hananils/kirby-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
100 lines (89 loc) · 3.03 KB
/
index.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
<?php
@include_once __DIR__ . '/vendor/autoload.php';
function getValue($value = null)
{
if (is_array($value)) {
return $value[0];
}
return $value;
}
Kirby::plugin('hananils/kirby-colors', [
'fields' => [
'colors' => [
'props' => [
'value' => function ($value = null) {
if (is_array($value)) {
return $value[0];
}
return $value;
},
'help' => function ($help = null) {
return $help;
},
'alpha' => function (bool $alpha = false) {
return $alpha;
},
'contrast' => function ($contrast = false) {
return $contrast;
}
],
'save' => function ($value) {
return $value;
}
]
],
'fieldMethods' => [
'isHex' => function ($field) {
$color = $field->toClass($field);
return $color->isHex($value);
},
'isRgb' => function ($field) {
$color = $field->toClass($field);
return $color->isRgb($value);
},
'isHsl' => function ($field) {
$color = $field->toClass($field);
return $color->isHsl($value);
},
'toClass' => function ($field) {
$value = getValue($field->value);
return new Hananils\Color($value);
},
'toColor' => function ($field, $space = null) {
$color = $field->toClass($field);
return $color->toString($space);
},
'toSpace' => function ($field) {
$color = $field->toClass($field);
return $color->toSpace();
},
'toValues' => function ($field) {
$color = $field->toClass($field);
return $color->toValues();
},
'toReadabilityReport' => function ($field) {
$color = $field->toClass($field);
$blueprint = $field->model()->blueprint()->fields();
$name = $field->key();
if (isset($blueprint[$name]['contrast']) && is_array($blueprint[$name]['contrast'])) {
return $color->toReadabilityReport($blueprint[$name]['contrast']);
}
return $color->toReadabilityReport();
},
'toMostReadable' => function ($field) {
$color = $field->toClass($field);
$blueprint = $field->model()->blueprint()->fields();
$name = $field->key();
if (isset($blueprint[$name]['contrast']) && is_array($blueprint[$name]['contrast'])) {
$readable = $color->toMostReadable($blueprint[$name]['contrast']);
} else {
$readable = $color->toMostReadable();
}
$space = $color->toSpace();
if (count($readable) === 0) {
return;
}
return array_shift($readable)['color']->toString($space);
}
]
]);