Passed
Push — master ( 090f34...a19fbb )
by Nicolaas
01:52
created

DBColour::ReadableColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The type TractorCow\Colorpicker\Color was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $isTransparent is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function CssClass(/** @scrutinizer ignore-unused */ ?bool $isTransparent = false): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $isTransparent is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
    public function CssClassAlternative(/** @scrutinizer ignore-unused */ ?bool $isTransparent = false): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
return $field is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return string.
Loading history...
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