From d0d3fd11771023619ad61953da09bc6808203ee0 Mon Sep 17 00:00:00 2001 From: Mattias Hinderson Date: Tue, 18 May 2021 17:44:50 +0200 Subject: [PATCH 1/3] Support the new Craft way of storing color fields (without `#` symbol) --- src/templates/_fields/colorit/input.twig | 14 ++++++++++++-- src/templates/_fields/colorit/preview.twig | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/templates/_fields/colorit/input.twig b/src/templates/_fields/colorit/input.twig index 0ee9f99..a8041df 100644 --- a/src/templates/_fields/colorit/input.twig +++ b/src/templates/_fields/colorit/input.twig @@ -20,7 +20,12 @@ handle == paletteColor.handle ? 'colorit--palette-colorIsSelected' : null, ]|filter|join(' ') %} - {% set processedColor = field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(paletteColor.color, opacity) : paletteColor.color %} + {% set color = paletteColor.color %} + {% if not (color starts with '#') %} + {% set color = '#' ~ color %} + {% endif %} + + {% set processedColor = field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(paletteColor.color, opacity) : color %}
  • @@ -42,7 +47,12 @@ {% if field.allowCustomColor %} - {% set processedColor = custom and field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(custom, opacity) : custom %} + {% set color = custom %} + {% if not (color starts with '#') %} + {% set color = '#' ~ color %} + {% endif %} + + {% set processedColor = custom and field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(custom, opacity) : color %}
    {{ forms.text({ diff --git a/src/templates/_fields/colorit/preview.twig b/src/templates/_fields/colorit/preview.twig index 7435921..7bd3ce6 100644 --- a/src/templates/_fields/colorit/preview.twig +++ b/src/templates/_fields/colorit/preview.twig @@ -19,7 +19,12 @@ craft.colorit.colors.hexIsTransparent(paletteColor.color) ? 'colorit--palette-colorIsTransparent' : null, ]|filter|join(' ') %} - {% set processedColor = field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(paletteColor.color, opacity) : paletteColor.color %} + {% set color = paletteColor.color %} + {% if not (color starts with '#') %} + {% set color = '#' ~ color %} + {% endif %} + + {% set processedColor = field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(paletteColor.color, opacity) : color %}
  • @@ -34,7 +39,12 @@ {% if field.allowCustomColor %} - {% set processedColor = custom and field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(custom, opacity) : custom %} + {% set color = custom %} + {% if not (color starts with '#') %} + {% set color = '#' ~ color %} + {% endif %} + + {% set processedColor = custom and field.allowOpacity and opacity < 100 ? craft.colorit.colors.hexToRgba(custom, opacity) : color %}
    From a014719fba0558f235876a4a844108a95a776628 Mon Sep 17 00:00:00 2001 From: Mattias Hinderson Date: Tue, 18 May 2021 17:55:38 +0200 Subject: [PATCH 2/3] Check for rgb as well --- src/templates/_fields/colorit/input.twig | 4 ++-- src/templates/_fields/colorit/preview.twig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/templates/_fields/colorit/input.twig b/src/templates/_fields/colorit/input.twig index a8041df..3c3e52c 100644 --- a/src/templates/_fields/colorit/input.twig +++ b/src/templates/_fields/colorit/input.twig @@ -21,7 +21,7 @@ ]|filter|join(' ') %} {% set color = paletteColor.color %} - {% if not (color starts with '#') %} + {% if not (color starts with '#' or color starts with 'rgb') %} {% set color = '#' ~ color %} {% endif %} @@ -48,7 +48,7 @@ {% if field.allowCustomColor %} {% set color = custom %} - {% if not (color starts with '#') %} + {% if not (color starts with '#' or color starts with 'rgb') %} {% set color = '#' ~ color %} {% endif %} diff --git a/src/templates/_fields/colorit/preview.twig b/src/templates/_fields/colorit/preview.twig index 7bd3ce6..373761e 100644 --- a/src/templates/_fields/colorit/preview.twig +++ b/src/templates/_fields/colorit/preview.twig @@ -20,7 +20,7 @@ ]|filter|join(' ') %} {% set color = paletteColor.color %} - {% if not (color starts with '#') %} + {% if not (color starts with '#' or color starts with 'rgb') %} {% set color = '#' ~ color %} {% endif %} @@ -40,7 +40,7 @@ {% if field.allowCustomColor %} {% set color = custom %} - {% if not (color starts with '#') %} + {% if not (color starts with '#' or color starts with 'rgb') %} {% set color = '#' ~ color %} {% endif %} From fd0fad4a3620c2c298bc321bb744b32c5d53e890 Mon Sep 17 00:00:00 2001 From: extensibleseth Date: Thu, 8 Jul 2021 17:03:39 -0700 Subject: [PATCH 3/3] Updated isValidHex() regex to validate hex values without a leading #. --- src/helpers/ColorHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/ColorHelper.php b/src/helpers/ColorHelper.php index bcba06b..227db36 100644 --- a/src/helpers/ColorHelper.php +++ b/src/helpers/ColorHelper.php @@ -107,7 +107,7 @@ public static function hexIsBlack(string $color) public static function isValidHex($color) { - return preg_match('/^#[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color) ? true : false; + return preg_match('/^#?[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color) ? true : false; } public static function hexToRgba($color, $opacity = false, $asArray = false)