1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SelectedColourPicker\ViewableData; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Forms\LiteralField; |
7
|
|
|
use SilverStripe\View\ViewableData; |
8
|
|
|
|
9
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
10
|
|
|
use Sunnysideup\SelectedColourPicker\Model\Fields\DBColour; |
11
|
|
|
|
12
|
|
|
class SelectedColourPickerFormFieldSwatches extends ViewableData |
13
|
|
|
{ |
14
|
|
|
public static function get_swatches_field(string $name, ?string $value, array $colours, bool $isBackgroundColour): LiteralField |
15
|
|
|
{ |
16
|
|
|
return LiteralField::create( |
17
|
|
|
'SwatchesFor' . $name, |
18
|
|
|
self::get_swatches_html($name, $value, $colours, $isBackgroundColour) |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function colour_and_background_swatch(string $colour, string $bgColour, ?bool $asHTML = true) |
23
|
|
|
{ |
24
|
|
|
$html = '<span style="color: '.$colour.'!important; background-color: '.$bgColour.'!important; padding: 4px; width: auto; text-align: center; border-radius: 4px; display: inline-block;">example</span>'; |
25
|
|
|
if($asHTML) { |
26
|
|
|
return DBField::create_field('HTMLText', $html); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function get_swatches_html(string $name, ?string $value, array $colours, bool $isBackgroundColour): string |
31
|
|
|
{ |
32
|
|
|
$options = static::get_swatches_field_inner((string) $value, (array) $colours, (bool) $isBackgroundColour); |
33
|
|
|
$id = $name . rand(0, 99999999999); |
34
|
|
|
$js = Convert::raw2att('document.querySelector("#' . $id . '").style.display="block";'); |
35
|
|
|
|
36
|
|
|
return ' |
37
|
|
|
<div class="field ' . $name . '-class"> |
38
|
|
|
<p> |
39
|
|
|
Current Value: ' . ($colours[$value] ?? $value) . ', '.$value.' |
40
|
|
|
<a onclick="' . $js . '" style="cursor: pointer;"><u>show available colours</u></a> |
41
|
|
|
</p> |
42
|
|
|
<div style="display: none" id="' . $id . '">' . implode('', $options) . '<hr style="clear: both; " /></div> |
43
|
|
|
</div>' |
44
|
|
|
; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function hex_invert(string $color): string |
48
|
|
|
{ |
49
|
|
|
$color = trim($color); |
50
|
|
|
$prependHash = false; |
51
|
|
|
if (false !== strpos($color, '#')) { |
52
|
|
|
$prependHash = true; |
53
|
|
|
$color = str_replace('#', '', $color); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$len = strlen($color); |
57
|
|
|
if (3 === $len || 6 === $len || 8 === $len) { |
58
|
|
|
if (8 === $len) { |
59
|
|
|
$color = substr($color, 0, 6); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (3 === $len) { |
63
|
|
|
$color = preg_replace('#(.)(.)(.)#', '\\1\\1\\2\\2\\3\\3', $color); |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
throw new \Exception("Invalid hex length ({$len}). Length must be 3 or 6 characters - colour is" . $color); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (! preg_match('#^[a-f0-9]{6}$#i', $color)) { |
70
|
|
|
throw new \Exception(sprintf('Invalid hex string #%s', htmlspecialchars($color, ENT_QUOTES))); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$r = dechex(255 - hexdec(substr($color, 0, 2))); |
|
|
|
|
74
|
|
|
$r = (strlen($r) > 1) ? $r : '0' . $r; |
75
|
|
|
|
76
|
|
|
$g = dechex(255 - hexdec(substr($color, 2, 2))); |
77
|
|
|
$g = (strlen($g) > 1) ? $g : '0' . $g; |
78
|
|
|
|
79
|
|
|
$b = dechex(255 - hexdec(substr($color, 4, 2))); |
80
|
|
|
$b = (strlen($b) > 1) ? $b : '0' . $b; |
81
|
|
|
|
82
|
|
|
return ($prependHash ? '#' : '') . $r . $g . $b; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected static function get_swatches_field_inner(string $value, array $colours, bool $isBackgroundColour): array |
86
|
|
|
{ |
87
|
|
|
$value = DBColour::check_colour($value, $isBackgroundColour); |
88
|
|
|
$ids = []; |
89
|
|
|
foreach ($colours as $colour => $name) { |
90
|
|
|
$invertColour = self::hex_invert($colour); |
91
|
|
|
if ($isBackgroundColour) { |
92
|
|
|
$styleA = 'background-color: ' . $colour . '; color: ' . $invertColour . '; '; |
93
|
|
|
} else { |
94
|
|
|
$styleA = 'color: ' . $colour . '; background-color: ' . $invertColour . ';'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$currentStyle = 'border: 4px solid transparent;'; |
98
|
|
|
if ($colour === $value) { |
99
|
|
|
$currentStyle = 'border: 4px solid red!important; border-radius: 0!important; font-weight: strong!important;'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$ids[$colour] = ' |
103
|
|
|
<div |
104
|
|
|
style="float: left; margin-right: 10px; margin-bottom: 10px; width: auto; border-radius: 30px; font-size: 12px; overflow: hidden; ' . $currentStyle . '" |
105
|
|
|
onMouseOver="this.style.borderRadius=\'0px\'" |
106
|
|
|
onMouseOut="this.style.borderRadius=\'30px\'" |
107
|
|
|
> |
108
|
|
|
<span style=" display: block; padding: 5px 15px; text-align: center; ' . $styleA . '"> |
109
|
|
|
' . $name . ' (' . $colour . ') |
110
|
|
|
</span> |
111
|
|
|
</div> |
112
|
|
|
'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $ids; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|