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