1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SelectedColourPicker\Model\Fields; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FormField; |
6
|
|
|
use SilverStripe\Forms\LiteralField; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
8
|
|
|
|
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use Sunnysideup\SelectedColourPicker\Forms\SelectedColourPickerFormField; |
11
|
|
|
use Sunnysideup\SelectedColourPicker\Forms\SelectedColourPickerFormFieldDropdown; |
12
|
|
|
use Sunnysideup\SelectedColourPicker\ViewableData\SelectedColourPickerFormFieldSwatches; |
13
|
|
|
use TractorCow\Colorpicker\Color; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class DBColour extends DBVarchar |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private static $colour_picker_field_class_name = SelectedColourPickerFormFieldDropdown::class; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* please set |
22
|
|
|
* must be defined as #AABB99 (hex codes). |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected const COLOURS = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* please set. |
30
|
|
|
* |
31
|
|
|
* @var string` |
32
|
|
|
*/ |
33
|
|
|
protected const CSS_CLASS_PREFIX = 'db-colour'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* please set. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected const IS_LIMITED_TO_OPTIONS = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* please set. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected const IS_BG_COLOUR = true; |
48
|
|
|
|
49
|
|
|
private static $casting = [ |
50
|
|
|
'CssClass' => 'Varchar', |
51
|
|
|
'ReadableColor' => 'Varchar' |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
public function __construct($name = null, $size = 9) |
55
|
|
|
{ |
56
|
|
|
parent::__construct($name, $size); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function CssClass(?bool $isTransparent = false): string |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return $this->getCssClass(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCssClass(?bool $isTransparent = false): string |
65
|
|
|
{ |
66
|
|
|
if($isTransparent) { |
67
|
|
|
$name = 'transparent'; |
68
|
|
|
} else { |
69
|
|
|
$name = static::COLOURS[$this->value] ?? 'colour-error'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->classCleanup($name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function CssClassAlternative(?bool $isTransparent = false): string |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return $this->getCssClassAlternative(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getCssClassAlternative(?bool $isTransparent = false): string |
81
|
|
|
{ |
82
|
|
|
if($isTransparent) { |
83
|
|
|
$name = 'ffffff00'; |
84
|
|
|
} else { |
85
|
|
|
$name = $this->value ?: 'no-colour'; |
86
|
|
|
} |
87
|
|
|
return $this->classCleanup($name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function scaffoldFormField($title = null, $params = null) |
91
|
|
|
{ |
92
|
|
|
return static::get_dropdown_field($this->name, $title); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* @param string $title |
99
|
|
|
* @return FormField |
100
|
|
|
*/ |
101
|
|
|
public static function get_dropdown_field(string $name, ?string $title = '') |
102
|
|
|
{ |
103
|
|
|
$className = Config::inst()->get(DBColour::class, 'colour_picker_field_class_name'); |
104
|
|
|
return $className::create( |
105
|
|
|
$name, |
106
|
|
|
$title |
107
|
|
|
) |
108
|
|
|
->setSource(static::COLOURS) |
109
|
|
|
->setLimitedToOptions(static::IS_LIMITED_TO_OPTIONS) |
110
|
|
|
->setIsBgColour(static::IS_BG_COLOUR); |
111
|
|
|
; |
112
|
|
|
return $field; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function get_swatches_field(string $name, string $value): LiteralField |
116
|
|
|
{ |
117
|
|
|
return SelectedColourPickerFormFieldSwatches::get_swatches_field((string) $name, (string) $value, static::COLOURS, static::IS_BG_COLOUR); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function ReadableColor(): string |
121
|
|
|
{ |
122
|
|
|
return $this->getReadableColor(); |
123
|
|
|
} |
124
|
|
|
public function getReadableColor(): string |
125
|
|
|
{ |
126
|
|
|
// Remove '#' if it's present |
127
|
|
|
$hexColor = ltrim((string) $this->value, '#'); |
128
|
|
|
|
129
|
|
|
// Check if the color is valid |
130
|
|
|
if(strlen($hexColor) == 6) { |
131
|
|
|
// Convert the color from hexadecimal to RGB |
132
|
|
|
$r = hexdec(substr($hexColor, 0, 2)) / 255; |
133
|
|
|
$g = hexdec(substr($hexColor, 2, 2)) / 255; |
134
|
|
|
$b = hexdec(substr($hexColor, 4, 2)) / 255; |
135
|
|
|
|
136
|
|
|
// Calculate the relative luminance |
137
|
|
|
$luminance = 0.2126 * $r + 0.7152 * $g + 0.0722 * $b; |
138
|
|
|
|
139
|
|
|
// Return either black or white, depending on the luminance |
140
|
|
|
if($luminance > 0.5) { |
141
|
|
|
return '#000000'; // Black color |
142
|
|
|
} else { |
143
|
|
|
return '#ffffff'; // White color |
144
|
|
|
} |
145
|
|
|
} else { |
146
|
|
|
return false; // Invalid color |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
private function classCleanup(string $name): string |
150
|
|
|
{ |
151
|
|
|
$name = str_replace('#', '', $name); |
152
|
|
|
$name = preg_replace('#[^A-Za-z0-9]#', '-', $name); |
153
|
|
|
|
154
|
|
|
return static::CSS_CLASS_PREFIX . '-' . trim(trim(strtolower($name), '-')); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// |
159
|
|
|
// public static function get_dropdown_field_old(?string $name = 'TextColour', ?string $title = 'Text Colour'): SelectedColourPickerFormField |
160
|
|
|
// { |
161
|
|
|
// $field = SelectedColourPickerFormField::create( |
162
|
|
|
// $name, |
163
|
|
|
// $title, |
164
|
|
|
// static::COLOURS |
165
|
|
|
// ); |
166
|
|
|
// $js = ' |
167
|
|
|
// jQuery("#TextAndBackgroudColourExample").css("color", jQuery(this).val()); |
168
|
|
|
// '; |
169
|
|
|
// $field->setAttribute('onchange', $js); |
170
|
|
|
// $field->setAttribute('onhover', $js); |
171
|
|
|
// $field->setAttribute('onclick', $js); |
172
|
|
|
// $field->setAttribute('onfocus', $js); |
173
|
|
|
// |
174
|
|
|
// return $field; |
175
|
|
|
// } |
176
|
|
|
// |
177
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths