Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshoerrmann committed Jul 18, 2019
2 parents b5a3be4 + c151e84 commit bd28502
Show file tree
Hide file tree
Showing 21 changed files with 1,595 additions and 158 deletions.
134 changes: 124 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,130 @@ colors:
## Field Methods
- `$field->isHex()`: wheather the color is stored as hexadecimal value, return `true` or `false`.
- `$field->isRgb()`: wheather the color is stored as RGB value, return `true` or `false`.
- `$field->isHsl()`: wheather the color is stored as HSL value, return `true` or `false`.
- `$field->toColors()`: returns the chosen color and the readable color.
- `$field->toColor()`: returns the chosen color.
- `$field->toReadableColor()`: returns the readable color.
#### `$field->isHex()`

Verifies if the current color is stored as hexadecimal value, returns `true` or `false`.

#### `$field->isRgb()`

Verifies if the current color is stored as RGB value, returns `true` or `false`.

#### `$field->isHsl()`

Verifies if the current color is stored as HSL value, returns `true` or `false`.

#### `$field->toClass()`

Returns the current color object, `Hananils\Color`, see `lib\Color.php`.

#### `$field->toColor($space)`

Returns the current color as string. Accepts an optional `$space` attribute to set the output color space, either `hex`, `rgb` or `hsl`.

#### `$field->toSpace()`

Returns the current color space, either `hex`, `rgb` or `hsl`.

#### `$field->toValues()`

Returns all color values, e. g.:

```php
[
'original' => '#ffffff',
'space' => 'hex',
'r' => 255,
'g' => 255,
'b' => 255,
'h' => 0,
's' => 0,
'l' => 100,
'a' => 100
]
```

#### `$field->toReadabilityReport()`

Returns a readability report for the contrast colors defined in the blueprint, defaults to black and white:

```php
Array
(
[color] => Hananils\Color Object
(
[original:Hananils\Color:private] => #00b7ff
[space:Hananils\Color:private] => hex
[r:Hananils\Color:private] => 0
[g:Hananils\Color:private] => 183
[b:Hananils\Color:private] => 255
[h:Hananils\Color:private] => 197
[s:Hananils\Color:private] => 100
[l:Hananils\Color:private] => 50
[a:Hananils\Color:private] => 100
)
[combinations] => Array
(
[0] => Array
(
[color] => Hananils\Color Object
(
[original:Hananils\Color:private] => #fff
[space:Hananils\Color:private] => hex
[r:Hananils\Color:private] => 255
[g:Hananils\Color:private] => 255
[b:Hananils\Color:private] => 255
[h:Hananils\Color:private] => 0
[s:Hananils\Color:private] => 0
[l:Hananils\Color:private] => 100
[a:Hananils\Color:private] => 100
)
[contrast] => 2.2783010917435
[accessibility] => Array
(
)
)
[1] => Array
(
[color] => Hananils\Color Object
(
[original:Hananils\Color:private] => #000
[space:Hananils\Color:private] => hex
[r:Hananils\Color:private] => 0
[g:Hananils\Color:private] => 0
[b:Hananils\Color:private] => 0
[h:Hananils\Color:private] => 0
[s:Hananils\Color:private] => 0
[l:Hananils\Color:private] => 0
[a:Hananils\Color:private] => 100
)
[contrast] => 9.2173945209011
[accessibility] => Array
(
[0] => aaLarge
[1] => aaaLarge
[2] => aa
[3] => aaa
)
)
)
)
```

#### `$field->toMostReadable()`

Returns the most readable color for the contrast colors defined in the blueprint, defaults to black and white.

## Color calculation

This plugin bundles two classes, one for JavaScript and one for PHP, with the identical API to perform color calculations. See `lib/Color.php` and `src/lib/color.js`.

## Tips and tricks

Expand All @@ -95,7 +213,3 @@ Please note that there is another color field for Kirby 3 developed by Tim Ötti
## License

MIT, [see license](https://github.com/hananils/kirby-colors/blob/master/LICENSE)

## Credits

Thanks to [Brian Grinstead](https://briangrinstead.com/) and [Scott Cooper](https://github.com/scttcper) for providing the [TinyColor library](https://github.com/TypeCtrl/tinycolor).
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Hananils\\": "lib/"
}
},
"require": {
"getkirby/composer-installer": "^1.1"
}
Expand Down
2 changes: 1 addition & 1 deletion index.js

Large diffs are not rendered by default.

78 changes: 60 additions & 18 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<?php

@include_once __DIR__ . '/vendor/autoload.php';

function getValue($value = null)
{
if (is_array($value)) {
return $value[0];
}

return $value;
}

Kirby::plugin('hananils/colors', [
'fields' => [
'colors' => [
'props' => [
'value' => function ($value = null) {
return Yaml::decode($value);
if (is_array($value)) {
return $value[0];
}

return $value;
},
'help' => function ($help = null) {
return $help;
Expand All @@ -18,41 +33,68 @@
}
],
'save' => function ($value) {
return Yaml::encode($value);
return $value;
}
]
],
'fieldMethods' => [
'isHex' => function ($field) {
$color = $field->toColor();
return strpos($color, '#') === 0;
$color = $field->toClass($field);
return $color->isHex($value);
},
'isRgb' => function ($field) {
return strpos($field->value, 'rgb') === 0;
$color = $field->toClass($field);
return $color->isRgb($value);
},
'isHsl' => function ($field) {
return strpos($field->value, 'hsl') === 0;
$color = $field->toClass($field);
return $color->isHsl($value);
},
'toClass' => function ($field) {
$value = getValue($field->value);
return new Hananils\Color($value);
},
'toColors' => function ($field) {
return Yaml::decode($field->value);
'toColor' => function ($field, $space = null) {
$color = $field->toClass($field);
return $color->toString($space);
},
'toColor' => function ($field) {
$colors = $field->toColors();
'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($colors[0])) {
return $colors[0];
if (isset($blueprint[$name]['contrast']) && is_array($blueprint[$name]['contrast'])) {
return $color->toReadabilityReport($blueprint[$name]['contrast']);
}

return null;
return $color->toReadabilityReport();
},
'toReadableColor' => function ($field) {
$colors = $field->toColors($field);
'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 (isset($colors[1])) {
return $colors[1];
if (count($readable) === 0) {
return;
}

return null;
return array_shift($readable)['color']->toString($space);
}
]
]);
Loading

0 comments on commit bd28502

Please sign in to comment.