├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bundle ├── DependencyInjection │ └── eZColorPickerExtension.php ├── FieldType │ └── ColorPicker │ │ ├── FormMapper.php │ │ ├── LegacyConverter.php │ │ ├── Type.php │ │ └── Value.php ├── Form │ └── Type │ │ ├── ColorPickerSettingsType.php │ │ └── ColorPickerType.php ├── Resources │ ├── config │ │ ├── fieldtype.yaml │ │ ├── prepend │ │ │ ├── ezplatform.yaml │ │ │ └── twig.yaml │ │ └── services.yaml │ ├── encore │ │ └── ez.config.manager.js │ ├── public │ │ ├── js │ │ │ └── admin │ │ │ │ └── codeincolor.js │ │ ├── pickr │ │ │ ├── dist │ │ │ │ ├── pickr.es5.min.js │ │ │ │ ├── pickr.es5.min.js.map │ │ │ │ ├── pickr.min.js │ │ │ │ ├── pickr.min.js.map │ │ │ │ └── themes │ │ │ │ │ ├── classic.min.css │ │ │ │ │ ├── monolith.min.css │ │ │ │ │ └── nano.min.css │ │ │ └── src │ │ │ │ └── scss │ │ │ │ ├── base.scss │ │ │ │ ├── lib │ │ │ │ ├── _mixins.scss │ │ │ │ └── _variables.scss │ │ │ │ └── themes │ │ │ │ ├── classic.scss │ │ │ │ ├── monolith.scss │ │ │ │ └── nano.scss │ │ └── scss │ │ │ └── admin │ │ │ ├── codeincolor-view.scss │ │ │ └── codeincolor.scss │ ├── translations │ │ ├── codeincolor_fieldtype.de.yml │ │ ├── codeincolor_fieldtype.en.yml │ │ ├── codeincolor_fieldtype.fr.yml │ │ ├── content_type.de.yml │ │ ├── content_type.en.yml │ │ ├── content_type.fr.yml │ │ ├── ezrepoforms_content_type.en.yaml │ │ ├── ezrepoforms_content_type.fr.yaml │ │ ├── fieldtypes.de.yaml │ │ ├── fieldtypes.en.yaml │ │ └── fieldtypes.fr.yaml │ └── views │ │ ├── admin │ │ ├── content_fields.html.twig │ │ └── field_template.html.twig │ │ └── standard │ │ └── content_fields.html.twig └── eZColorPickerBundle.php ├── composer.json ├── lib └── ColorConverter │ ├── Color │ ├── HEX.php │ ├── HEXa.php │ ├── HSVa.php │ ├── RGB.php │ └── RGBa.php │ └── ColorConverter.php ├── package-lock.json ├── package.json ├── phpunit.xml └── tests ├── bundle ├── DependencyInjection │ └── eZColorPickerExtensionTest.php └── FieldType │ └── ColorPicker │ ├── TypeTest.php │ └── ValueTest.php └── lib └── ColorConverter ├── ColorConverterTest.php └── ColorPickerDataFixtures.php /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .phpunit.result.cache 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [2.0.0] - 2021-07-09 8 | ### Changed 9 | - Changed clear button label #13 10 | ### Fixed 11 | - Fix exception when building forms #12 12 | 13 | ## [1.0.1] - 2020-09-23 14 | ### Fixed 15 | - Package no longer require `dev` minimum stability. 16 | 17 | ## [1.0.0] - 2020-09-23 18 | ### Added 19 | - `codeincolor` Field Type featuring a color picker 20 | - Color converter service 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Codein 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codein eZColorPicker 2 | 3 | Symfony bundle that adds color management to eZ Platform. 4 | 5 | **Note** : this is the compatibility branch for eZ Platform 2.5 (ezsystems/ezpublish-kernel:7.5). For a Ibexa Platform compatible version, please check this repository instead : https://github.com/Codein-Labs/ibexa-color-picker 6 | 7 | ## Installation 8 | 9 | ``` 10 | composer require codein/ez-color-picker:dev-develop 11 | ``` 12 | 13 | Activate the bundle in AppKernel.php 14 | 15 | ``` 16 | new Codein\eZColorPicker\eZColorPickerBundle() 17 | ``` 18 | 19 | Compile the assets for the admin UI 20 | 21 | ``` 22 | ./bin/console ezplatform:encore:compile 23 | ``` 24 | 25 | ## Usage 26 | 27 | ### Default value 28 | 29 | You can set a default color for your field. The default color will be proposed to the user if no color is already set. 30 | If the field is required, the default color is assigned as default. If not, no color is assigned. 31 | 32 | ### Twig 33 | 34 | The default color format rendered in Twig is RGBa. You can pass extra options to ez_render_field. 35 | 36 | * format: RGBa, HEXa, HSVa, RGB, HEX 37 | * default: default value returned if field is empty. If not set ez_render_field will return an empty string 38 | 39 | ```twig 40 | {{ ez_render_field(content, 'color2', {'parameters': {'format': 'HEX', 'default': 'none'}}) }} 41 | ``` 42 | 43 | ### Migration 44 | 45 | The method \Codein\eZColorPicker\FieldType\ColorPicker\Type::acceptValue will accept a single string and convert it into 46 | a color. Following formats are converted into a valid value object : 47 | 48 | ``` 49 | HSVa: hsva(0, 86%, 69%, 0.69) 50 | RGBa: rgba(176, 25, 25, 0.69) 51 | HEXa: #B01919B0 52 | RGB: rgb(176, 25, 25) 53 | HEX: #B01919 54 | ``` 55 | 56 | You can use this feature when writing migrations. 57 | 58 | We also provide a color converter service for your needs [here](lib/ColorConverter/ColorConverter.php). 59 | 60 | ### Acknowledgement 61 | 62 | This bundle uses Pickr (https://github.com/Simonwep/pickr) under the MIT Licence. 63 | -------------------------------------------------------------------------------- /bundle/DependencyInjection/eZColorPickerExtension.php: -------------------------------------------------------------------------------- 1 | load('services.yaml'); 24 | $loader->load('fieldtype.yaml'); 25 | } 26 | 27 | public function prepend(ContainerBuilder $container) 28 | { 29 | $coreExtensionConfigFile = realpath(__DIR__ . '/../Resources/config/prepend/ezplatform.yaml'); 30 | $container->prependExtensionConfig('ezpublish', Yaml::parseFile($coreExtensionConfigFile)); 31 | $container->addResource(new FileResource($coreExtensionConfigFile)); 32 | 33 | $twigExtensionConfigFile = realpath(__DIR__ . '/../Resources/config/prepend/twig.yaml'); 34 | $container->prependExtensionConfig('twig', Yaml::parseFile($twigExtensionConfigFile)); 35 | $container->addResource(new FileResource($twigExtensionConfigFile)); 36 | } 37 | 38 | 39 | } -------------------------------------------------------------------------------- /bundle/FieldType/ColorPicker/FormMapper.php: -------------------------------------------------------------------------------- 1 | add( 20 | $fieldDefinitionForm->getConfig()->getFormFactory()->createBuilder() 21 | ->create( 22 | 'defaultValue', 23 | ColorPickerType::class, [ 24 | 'required' => false, 25 | 'label' => 'codeincolor.default.color', 26 | ] 27 | ) 28 | ->setAutoInitialize(false)->getForm() 29 | ); 30 | } 31 | 32 | public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) 33 | { 34 | $definition = $data->fieldDefinition; 35 | $fieldForm->add('value', ColorPickerType::class, [ 36 | 'required' => $definition->isRequired, 37 | 'label' => $definition->getName(), 38 | 'defaultValue' => $definition->defaultValue, 39 | ]); 40 | } 41 | } -------------------------------------------------------------------------------- /bundle/FieldType/ColorPicker/LegacyConverter.php: -------------------------------------------------------------------------------- 1 | dataText = json_encode($value->data); 18 | $storageFieldValue->sortKeyString = $value->sortKey; 19 | } 20 | 21 | public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) 22 | { 23 | $fieldValue->data = json_decode($value->dataText, true); 24 | $fieldValue->sortKey = $value->sortKeyString; 25 | } 26 | 27 | public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) 28 | { 29 | $storageDef->dataText1 = json_encode($fieldDef->defaultValue->data); 30 | } 31 | 32 | public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) 33 | { 34 | $fieldDef->defaultValue->data = json_decode($storageDef->dataText1, true); 35 | } 36 | 37 | public function getIndexColumn() 38 | { 39 | return 'sort_key_string'; 40 | } 41 | } -------------------------------------------------------------------------------- /bundle/FieldType/ColorPicker/Type.php: -------------------------------------------------------------------------------- 1 | colorConverter = $colorConverter; 24 | } 25 | 26 | public function getFieldTypeIdentifier(): string 27 | { 28 | return 'codeincolor'; 29 | } 30 | 31 | public function getSettingsSchema(): array 32 | { 33 | return [ 34 | 'defaultValue' => [ 35 | 'type' => ColorPickerValue::class, 36 | 'default' => new ColorPickerValue(), 37 | ], 38 | ]; 39 | } 40 | 41 | public function validateFieldSettings($fieldSettings) 42 | { 43 | return []; 44 | } 45 | 46 | protected function createValueFromInput($inputValue) 47 | { 48 | if($inputValue instanceof ColorPickerValue) { 49 | return $inputValue; 50 | } elseif (is_array($inputValue)) { 51 | return (new ColorPickerValue())->setValueFromHash($inputValue); 52 | } 53 | 54 | $value = new ColorPickerValue(); 55 | if(is_string($inputValue) && $this->colorConverter->stringIsColor($inputValue)) { 56 | $HSVa = $this->colorConverter->convertStringToHSVa($inputValue); 57 | $value->HSVa = (string)$HSVa; 58 | $value->RGBa = (string)$this->colorConverter->convertHSVaToRGBa($HSVa); 59 | $value->HEXa = (string)$this->colorConverter->convertHSVaToHEXa($HSVa); 60 | $value->RGB = (string)$this->colorConverter->convertHSVaToRGB($HSVa); 61 | $value->HEX = (string)$this->colorConverter->convertHSVaToHEX($HSVa); 62 | } 63 | return $value; 64 | } 65 | 66 | protected function checkValueStructure(BaseValue $value) 67 | { 68 | } 69 | 70 | public function getName(Value $value) 71 | { 72 | return (string)$value; 73 | } 74 | 75 | public function getEmptyValue() 76 | { 77 | return new ColorPickerValue(); 78 | } 79 | 80 | public function fromHash($hash) 81 | { 82 | $value = new ColorPickerValue(); 83 | if(is_array($hash)) { 84 | $value->setValueFromHash($hash); 85 | } 86 | return $value; 87 | } 88 | 89 | public function toHash(Value $value) 90 | { 91 | if($value instanceof ColorPickerValue) { 92 | return $value->getValueAsHash(); 93 | } 94 | return []; 95 | } 96 | 97 | public function getFieldName(Value $value, FieldDefinition $fieldDefinition, $languageCode) 98 | { 99 | return (string)$value; 100 | } 101 | 102 | protected function getSortInfo(BaseValue $value) 103 | { 104 | return (string)$value; 105 | } 106 | } -------------------------------------------------------------------------------- /bundle/FieldType/ColorPicker/Value.php: -------------------------------------------------------------------------------- 1 | $value) { 25 | if(property_exists($this, $key)) { 26 | $this->{$key} = $value; 27 | } 28 | } 29 | return $this; 30 | } 31 | 32 | public function getValueAsHash() 33 | { 34 | return get_object_vars($this); 35 | } 36 | 37 | public function __toString() 38 | { 39 | return (string)$this->RGBa; 40 | } 41 | 42 | public function isEmpty() 43 | { 44 | foreach ($this as $key => $value) { 45 | if($this->{$key} === null) { 46 | return true; 47 | } 48 | } 49 | return false; 50 | } 51 | 52 | public function getValueInFormat($format) 53 | { 54 | return $this->{$format}; 55 | } 56 | } -------------------------------------------------------------------------------- /bundle/Form/Type/ColorPickerSettingsType.php: -------------------------------------------------------------------------------- 1 | add('defaultValue', ColorPickerType::class, [ 19 | 'required' => false, 20 | 'useViewTransformer' => true, 21 | 'label' => 'codeincolor.default.color', 22 | ])->setAutoInitialize(false); 23 | } 24 | } -------------------------------------------------------------------------------- /bundle/Form/Type/ColorPickerType.php: -------------------------------------------------------------------------------- 1 | add($field, TextType::class, [ 23 | 'label' => sprintf('codeincolor.pickr.color.%s', strtolower($field)), 24 | 'translation_domain' => 'codeincolor_fieldtype', 25 | 'attr' => [ 26 | 'readonly' => 'readonly' 27 | ] 28 | ]); 29 | } 30 | 31 | /** 32 | * If the value is empty and the field is required and has a default value 33 | * We need to override the current value with the default one 34 | */ 35 | if(is_array($options['defaultValue']) && $options['required']) { 36 | $defaultValue = $options['defaultValue']; 37 | $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($defaultValue) { 38 | /** @var ColorPickerValue $colorPickerValue */ 39 | $colorPickerValue = $event->getData(); 40 | $defaultValue = (new ColorPickerValue())->setValueFromHash($defaultValue); 41 | if($colorPickerValue->isEmpty() && !$defaultValue->isEmpty()) { 42 | $event->setData($defaultValue); 43 | } 44 | }); 45 | } 46 | 47 | /** 48 | * Need to use à view transformer in order to use this type to set the default value 49 | * with a color picker 50 | */ 51 | if($options['useViewTransformer']) { 52 | $builder->addViewTransformer(new CallbackTransformer( 53 | function ($value) { 54 | $colorPickerValue = new ColorPickerValue(); 55 | if(is_array($value)) { 56 | $colorPickerValue->setValueFromHash($value); 57 | } 58 | return $colorPickerValue; 59 | }, 60 | function ($value) { 61 | if ($value instanceof ColorPickerValue) { 62 | return $value->getValueAsHash(); 63 | } 64 | $colorPickerValue = new ColorPickerValue(); 65 | return $colorPickerValue->getValueAsHash(); 66 | } 67 | )); 68 | } 69 | } 70 | 71 | public function buildView(FormView $view, FormInterface $form, array $options) 72 | { 73 | $view->vars = array_merge($view->vars, [ 74 | 'formParams' => \json_encode([ 75 | 'required' => $options['required'], 76 | 'defaultValue' => $options['defaultValue'] 77 | ]) 78 | ]); 79 | parent::buildView($view, $form, $options); 80 | } 81 | 82 | public function configureOptions(OptionsResolver $resolver): void 83 | { 84 | $resolver->setDefaults([ 85 | 'data_class' => ColorPickerValue::class, 86 | 'useViewTransformer' => false, 87 | 'defaultValue' => null, 88 | ]); 89 | } 90 | 91 | public function getBlockPrefix() 92 | { 93 | return 'codeincolor'; 94 | } 95 | } -------------------------------------------------------------------------------- /bundle/Resources/config/fieldtype.yaml: -------------------------------------------------------------------------------- 1 | parameters: 2 | codein.fieldtype.color.identifier: 'codeincolor' 3 | 4 | services: 5 | codein.ezcolorpikcerbundle.fieldtype.codeincolor: 6 | parent: ezpublish.fieldType 7 | class: Codein\eZColorPicker\FieldType\ColorPicker\Type 8 | calls: 9 | - [ 'setColorConverter', [ "@codein.ezcolorpikcerbundle.colorconverter" ] ] 10 | tags: 11 | - { name: ezpublish.fieldType, alias: "%codein.fieldtype.color.identifier%" } 12 | - { name: ezpublish.fieldType.nameable, alias: "%codein.fieldtype.color.identifier%" } 13 | codein.ezcolorpikcerbundle.fieldtype.converter: 14 | class: Codein\eZColorPicker\FieldType\ColorPicker\LegacyConverter 15 | tags: 16 | - { name: ezpublish.storageEngine.legacy.converter, alias: "%codein.fieldtype.color.identifier%" } 17 | codein.ezcolorpikcerbundle.fieldtype.form_mapper: 18 | class: Codein\eZColorPicker\FieldType\ColorPicker\FormMapper 19 | tags: 20 | - { name: ez.fieldFormMapper.value, fieldType: "%codein.fieldtype.color.identifier%" } 21 | - { name: ez.fieldFormMapper.definition, fieldType: "%codein.fieldtype.color.identifier%" } 22 | codein.ezcolorpikcerbundle.fieldtype.unindexed: 23 | class: eZ\Publish\Core\FieldType\Unindexed 24 | tags: 25 | - { name: ezpublish.fieldType.indexable, alias: "%codein.fieldtype.color.identifier%" } -------------------------------------------------------------------------------- /bundle/Resources/config/prepend/ezplatform.yaml: -------------------------------------------------------------------------------- 1 | system: 2 | admin_group: 3 | field_templates: 4 | - template: '@eZColorPicker/admin/content_fields.html.twig' 5 | priority: 1 6 | default: 7 | field_templates: 8 | - template: '@eZColorPicker/standard/content_fields.html.twig' 9 | priority: 1 10 | -------------------------------------------------------------------------------- /bundle/Resources/config/prepend/twig.yaml: -------------------------------------------------------------------------------- 1 | form_themes: 2 | - '@eZColorPicker/admin/field_template.html.twig' 3 | -------------------------------------------------------------------------------- /bundle/Resources/config/services.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | codein.ezcolorpikcerbundle.colorconverter: 3 | class: Codein\ColorConverter\ColorConverter 4 | autowire: true 5 | public: true 6 | -------------------------------------------------------------------------------- /bundle/Resources/encore/ez.config.manager.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = (eZConfig, eZConfigManager) => { 4 | eZConfigManager.add({ 5 | eZConfig, 6 | entryName: 'ezplatform-admin-ui-layout-css', 7 | newItems: [path.resolve(__dirname, '../public/scss/admin/codeincolor.scss')], 8 | }); 9 | eZConfigManager.add({ 10 | eZConfig, 11 | entryName: 'ezplatform-admin-ui-location-view-css', 12 | newItems: [path.resolve(__dirname, '../public/scss/admin/codeincolor-view.scss')], 13 | }); 14 | eZConfigManager.add({ 15 | eZConfig, 16 | entryName: 'ezplatform-admin-ui-content-type-edit-js', 17 | newItems: [path.resolve(__dirname, '../public/js/admin/codeincolor.js')], 18 | }); 19 | eZConfigManager.add({ 20 | eZConfig, 21 | entryName: 'ezplatform-admin-ui-content-edit-parts-js', 22 | newItems: [path.resolve(__dirname, '../public/js/admin/codeincolor.js')], 23 | }); 24 | }; -------------------------------------------------------------------------------- /bundle/Resources/public/js/admin/codeincolor.js: -------------------------------------------------------------------------------- 1 | import '../../pickr/dist/themes/classic.min.css'; 2 | import Pickr from '../../pickr/dist/pickr.es5.min'; 3 | 4 | (function(global) { 5 | global.codeinColor = { 6 | formParams: [], 7 | createPickr: function(params) { 8 | let containerElt = this.getContainer(params.container) 9 | let buttonElt = containerElt.querySelector('.codeincolor__button') 10 | let currentValue = this.getInputValue(containerElt, 'RGBa') 11 | let currentColor = null 12 | if(currentValue.length > 0) { 13 | currentColor = currentValue 14 | } 15 | 16 | this.formParams[params.container] = params.formParams 17 | 18 | let pickr = Pickr.create({ 19 | theme: 'classic', 20 | el: buttonElt, 21 | default: currentColor, 22 | components: { 23 | // Main components 24 | preview: true, 25 | opacity: true, 26 | hue: true, 27 | 28 | // Input / output Options 29 | interaction: { 30 | hex: true, 31 | rgba: true, 32 | hsla: true, 33 | hsva: true, 34 | cmyk: true, 35 | input: true, 36 | clear: true, 37 | cancel: true, 38 | save: true 39 | } 40 | }, 41 | i18n: params.i18n 42 | }); 43 | pickr.on('show', (color, instance) => { 44 | let containerId = instance.options.el.getAttribute('data-pickr-container-id') 45 | let container = this.getContainer(containerId) 46 | if(this.formParams[containerId] !== undefined && this.getInputValue(container, "HEXa").length === 0) { 47 | let defaultColor = '#106d95FF' 48 | if(this.formParams[containerId].defaultValue !== null 49 | && this.formParams[containerId].defaultValue.hasOwnProperty('HEXa') 50 | && this.formParams[containerId].defaultValue.HEXa !== null) { 51 | defaultColor = this.formParams[containerId].defaultValue.HEXa 52 | } 53 | let colorRepresentation = instance.getColorRepresentation() 54 | let element = instance.getRoot().app.querySelector('input.pcr-result') 55 | let event = new Event('input') 56 | element.value = defaultColor 57 | element.dispatchEvent(event) 58 | if(colorRepresentation !== undefined) { 59 | instance.setColorRepresentation(colorRepresentation) 60 | } 61 | } 62 | }) 63 | pickr.on('save', (color, instance) => { 64 | let container = this.getContainer(instance.options.el.getAttribute('data-pickr-container-id')) 65 | if(container !== null) { 66 | if(color === null) { 67 | this.setInputValue(container, 'RGBa', "") 68 | this.setInputValue(container, 'HEXa', "") 69 | this.setInputValue(container, 'HSVa', "") 70 | this.setInputValue(container, 'RGB', "") 71 | this.setInputValue(container, 'HEX', "") 72 | } else { 73 | this.setInputValue(container, 'RGBa', color.toRGBA().toString(0)) 74 | this.setInputValue(container, 'HEXa', color.toHEXA().toString(0)) 75 | this.setInputValue(container, 'HSVa', color.toHSVA().toString(0)) 76 | this.setInputValue(container, 'RGB', this.getRGBValue(color)) 77 | this.setInputValue(container, 'HEX', this.getHEXValue(color)) 78 | } 79 | } 80 | instance.hide(); 81 | }) 82 | pickr.on('cancel', (color, instance) => { 83 | instance.hide(); 84 | }) 85 | }, 86 | getContainer: function(containerId) { 87 | return document.getElementById(containerId) 88 | }, 89 | getInput: function(container, colorType) { 90 | return container.querySelector("[id$='" + colorType + "']") 91 | }, 92 | getInputValue: function(container, colorType) { 93 | let input = this.getInput(container, colorType) 94 | if(input !== null) { 95 | return input.value 96 | } 97 | return ""; 98 | }, 99 | setInputValue: function(container, colorType, value) { 100 | let input = this.getInput(container, colorType) 101 | if(input !== null) { 102 | input.value = value 103 | } 104 | }, 105 | getRGBValue: function (color) { 106 | let RGBa = color.toRGBA() 107 | return 'rgb(' + Math.round(RGBa[0]) + ', ' + Math.round(RGBa[1]) + ', ' + Math.round(RGBa[2]) + ')' 108 | }, 109 | getHEXValue: function (color) { 110 | let HEXa = color.toHEXA() 111 | let HEXString = '#' + HEXa[0] + '' + HEXa[1] + '' + HEXa[2] 112 | return HEXString.toUpperCase() 113 | }, 114 | } 115 | })(window, window.document, window.eZ); 116 | -------------------------------------------------------------------------------- /bundle/Resources/public/pickr/dist/pickr.es5.min.js: -------------------------------------------------------------------------------- 1 | /*! Pickr 1.7.4 MIT | https://github.com/Simonwep/pickr */ 2 | !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Pickr=e():t.Pickr=e()}(window,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=145)}([function(t,e,r){var n=r(3),o=r(15).f,i=r(12),a=r(17),c=r(48),u=r(81),s=r(85);t.exports=function(t,e){var r,l,f,p,v,h=t.target,d=t.global,g=t.stat;if(r=d?n:g?n[h]||c(h,{}):(n[h]||{}).prototype)for(l in e){if(p=e[l],f=t.noTargetGet?(v=o(r,l))&&v.value:r[l],!s(d?l:h+(g?".":"#")+l,t.forced)&&void 0!==f){if(typeof p==typeof f)continue;u(p,f)}(t.sham||f&&f.sham)&&i(p,"sham",!0),a(r,l,p,t)}}},function(t,e){t.exports=function(t){try{return!!t()}catch(t){return!0}}},function(t,e,r){var n=r(3),o=r(49),i=r(4),a=r(50),c=r(55),u=r(86),s=o("wks"),l=n.Symbol,f=u?l:l&&l.withoutSetter||a;t.exports=function(t){return i(s,t)||(c&&i(l,t)?s[t]=l[t]:s[t]=f("Symbol."+t)),s[t]}},function(t,e,r){(function(e){var r=function(t){return t&&t.Math==Math&&t};t.exports=r("object"==typeof globalThis&&globalThis)||r("object"==typeof window&&window)||r("object"==typeof self&&self)||r("object"==typeof e&&e)||Function("return this")()}).call(this,r(111))},function(t,e){var r={}.hasOwnProperty;t.exports=function(t,e){return r.call(t,e)}},function(t,e,r){var n=r(1);t.exports=!n((function(){return 7!=Object.defineProperty({},1,{get:function(){return 7}})[1]}))},function(t,e,r){var n=r(7);t.exports=function(t){if(!n(t))throw TypeError(String(t)+" is not an object");return t}},function(t,e){t.exports=function(t){return"object"==typeof t?null!==t:"function"==typeof t}},function(t,e,r){var n=r(18),o=Math.min;t.exports=function(t){return t>0?o(n(t),9007199254740991):0}},function(t,e,r){var n=r(5),o=r(77),i=r(6),a=r(22),c=Object.defineProperty;e.f=n?c:function(t,e,r){if(i(t),e=a(e,!0),i(r),o)try{return c(t,e,r)}catch(t){}if("get"in r||"set"in r)throw TypeError("Accessors not supported");return"value"in r&&(t[e]=r.value),t}},function(t,e,r){var n=r(21),o=r(11);t.exports=function(t){return n(o(t))}},function(t,e){t.exports=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t}},function(t,e,r){var n=r(5),o=r(9),i=r(20);t.exports=n?function(t,e,r){return o.f(t,e,i(1,r))}:function(t,e,r){return t[e]=r,t}},function(t,e,r){var n=r(11);t.exports=function(t){return Object(n(t))}},function(t,e,r){var n=r(5),o=r(1),i=r(4),a=Object.defineProperty,c={},u=function(t){throw t};t.exports=function(t,e){if(i(c,t))return c[t];e||(e={});var r=[][t],s=!!i(e,"ACCESSORS")&&e.ACCESSORS,l=i(e,0)?e[0]:u,f=i(e,1)?e[1]:void 0;return c[t]=!!r&&!o((function(){if(s&&!n)return!0;var t={length:-1};s?a(t,1,{enumerable:!0,get:u}):t[1]=1,r.call(t,l,f)}))}},function(t,e,r){var n=r(5),o=r(47),i=r(20),a=r(10),c=r(22),u=r(4),s=r(77),l=Object.getOwnPropertyDescriptor;e.f=n?l:function(t,e){if(t=a(t),e=c(e,!0),s)try{return l(t,e)}catch(t){}if(u(t,e))return i(!o.f.call(t,e),t[e])}},function(t,e){var r={}.toString;t.exports=function(t){return r.call(t).slice(8,-1)}},function(t,e,r){var n=r(3),o=r(12),i=r(4),a=r(48),c=r(79),u=r(31),s=u.get,l=u.enforce,f=String(String).split("String");(t.exports=function(t,e,r,c){var u=!!c&&!!c.unsafe,s=!!c&&!!c.enumerable,p=!!c&&!!c.noTargetGet;"function"==typeof r&&("string"!=typeof e||i(r,"name")||o(r,"name",e),l(r).source=f.join("string"==typeof e?e:"")),t!==n?(u?!p&&t[e]&&(s=!0):delete t[e],s?t[e]=r:o(t,e,r)):s?t[e]=r:a(e,r)})(Function.prototype,"toString",(function(){return"function"==typeof this&&s(this).source||c(this)}))},function(t,e){var r=Math.ceil,n=Math.floor;t.exports=function(t){return isNaN(t=+t)?0:(t>0?n:r)(t)}},function(t,e,r){var n=r(89),o=r(21),i=r(13),a=r(8),c=r(58),u=[].push,s=function(t){var e=1==t,r=2==t,s=3==t,l=4==t,f=6==t,p=5==t||f;return function(v,h,d,g){for(var y,b,m=i(v),x=o(m),S=n(h,d,3),w=a(x.length),O=0,_=g||c,A=e?_(v,w):r?_(v,0):void 0;w>O;O++)if((p||O in x)&&(b=S(y=x[O],O,m),t))if(e)A[O]=b;else if(b)switch(t){case 3:return!0;case 5:return y;case 6:return O;case 2:u.call(A,y)}else if(l)return!1;return f?-1:s||l?l:A}};t.exports={forEach:s(0),map:s(1),filter:s(2),some:s(3),every:s(4),find:s(5),findIndex:s(6)}},function(t,e){t.exports=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}}},function(t,e,r){var n=r(1),o=r(16),i="".split;t.exports=n((function(){return!Object("z").propertyIsEnumerable(0)}))?function(t){return"String"==o(t)?i.call(t,""):Object(t)}:Object},function(t,e,r){var n=r(7);t.exports=function(t,e){if(!n(t))return t;var r,o;if(e&&"function"==typeof(r=t.toString)&&!n(o=r.call(t)))return o;if("function"==typeof(r=t.valueOf)&&!n(o=r.call(t)))return o;if(!e&&"function"==typeof(r=t.toString)&&!n(o=r.call(t)))return o;throw TypeError("Can't convert object to primitive value")}},function(t,e){t.exports=!1},function(t,e,r){"use strict";var n=r(22),o=r(9),i=r(20);t.exports=function(t,e,r){var a=n(e);a in t?o.f(t,a,i(0,r)):t[a]=r}},function(t,e,r){var n=r(1),o=r(2),i=r(90),a=o("species");t.exports=function(t){return i>=51||!n((function(){var e=[];return(e.constructor={})[a]=function(){return{foo:1}},1!==e[t](Boolean).foo}))}},function(t,e,r){"use strict";var n=r(1);t.exports=function(t,e){var r=[][t];return!!r&&n((function(){r.call(null,e||function(){throw 1},1)}))}},function(t,e){t.exports={}},function(t,e,r){var n=r(65),o=r(17),i=r(130);n||o(Object.prototype,"toString",i,{unsafe:!0})},function(t,e,r){"use strict";var n=r(0),o=r(44);n({target:"RegExp",proto:!0,forced:/./.exec!==o},{exec:o})},function(t,e,r){"use strict";var n=r(17),o=r(6),i=r(1),a=r(101),c=RegExp.prototype,u=c.toString,s=i((function(){return"/a/b"!=u.call({source:"a",flags:"b"})})),l="toString"!=u.name;(s||l)&&n(RegExp.prototype,"toString",(function(){var t=o(this),e=String(t.source),r=t.flags;return"/"+e+"/"+String(void 0===r&&t instanceof RegExp&&!("flags"in c)?a.call(t):r)}),{unsafe:!0})},function(t,e,r){var n,o,i,a=r(112),c=r(3),u=r(7),s=r(12),l=r(4),f=r(32),p=r(33),v=c.WeakMap;if(a){var h=new v,d=h.get,g=h.has,y=h.set;n=function(t,e){return y.call(h,t,e),e},o=function(t){return d.call(h,t)||{}},i=function(t){return g.call(h,t)}}else{var b=f("state");p[b]=!0,n=function(t,e){return s(t,b,e),e},o=function(t){return l(t,b)?t[b]:{}},i=function(t){return l(t,b)}}t.exports={set:n,get:o,has:i,enforce:function(t){return i(t)?o(t):n(t,{})},getterFor:function(t){return function(e){var r;if(!u(e)||(r=o(e)).type!==t)throw TypeError("Incompatible receiver, "+t+" required");return r}}}},function(t,e,r){var n=r(49),o=r(50),i=n("keys");t.exports=function(t){return i[t]||(i[t]=o(t))}},function(t,e){t.exports={}},function(t,e,r){var n=r(83),o=r(3),i=function(t){return"function"==typeof t?t:void 0};t.exports=function(t,e){return arguments.length<2?i(n[t])||i(o[t]):n[t]&&n[t][e]||o[t]&&o[t][e]}},function(t,e,r){var n=r(84),o=r(53).concat("length","prototype");e.f=Object.getOwnPropertyNames||function(t){return n(t,o)}},function(t,e,r){var n=r(16);t.exports=Array.isArray||function(t){return"Array"==n(t)}},function(t,e,r){var n,o=r(6),i=r(113),a=r(53),c=r(33),u=r(114),s=r(78),l=r(32),f=l("IE_PROTO"),p=function(){},v=function(t){return" 33 | {% endblock %} 34 | -------------------------------------------------------------------------------- /bundle/Resources/views/standard/content_fields.html.twig: -------------------------------------------------------------------------------- 1 | {%- block codeincolor_field -%} 2 | {% apply spaceless %} 3 | {% set format = parameters.format|default(constant('Codein\\eZColorPicker\\FieldType\\ColorPicker\\Value::FORMAT_RGBA')) %} 4 | {% if parameters.default is defined and field.value.isEmpty() %} 5 | {{ parameters.default }} 6 | {% else %} 7 | {{ field.value.getValueInFormat(format) }} 8 | {% endif %} 9 | {% endapply %} 10 | {% endblock -%} -------------------------------------------------------------------------------- /bundle/eZColorPickerBundle.php: -------------------------------------------------------------------------------- 1 | HEXa) == 9) { 12 | return substr($this->HEXa, 0, 7); 13 | } 14 | return ""; 15 | } 16 | } -------------------------------------------------------------------------------- /lib/ColorConverter/Color/HEXa.php: -------------------------------------------------------------------------------- 1 | HEXa = '#'; 14 | $this->HEXa .= str_pad(dechex($RGBa->R), 2, 0, STR_PAD_LEFT); 15 | $this->HEXa .= str_pad(dechex($RGBa->G), 2, 0, STR_PAD_LEFT); 16 | $this->HEXa .= str_pad(dechex($RGBa->B), 2, 0, STR_PAD_LEFT); 17 | $this->HEXa .= str_pad(dechex(round($RGBa->a*255)), 2, 0, STR_PAD_LEFT); 18 | $this->HEXa = strtoupper($this->HEXa); 19 | return $this; 20 | } 21 | 22 | public function __toString() 23 | { 24 | if (strlen($this->HEXa) == 9) { 25 | return $this->HEXa; 26 | } 27 | return ""; 28 | } 29 | } -------------------------------------------------------------------------------- /lib/ColorConverter/Color/HSVa.php: -------------------------------------------------------------------------------- 1 | fromHSVaMatches($matches); 17 | } 18 | 19 | public static function getFromRGBaMatches(array $matches) 20 | { 21 | return (new HSVa())->fromRGBaMatches($matches); 22 | } 23 | 24 | public static function getFromHEXaMatches(array $matches) 25 | { 26 | return (new HSVa())->fromHEXaMatches($matches); 27 | } 28 | 29 | protected function fromHSVaMatches($matches) 30 | { 31 | $this->H = $matches[3]; 32 | $this->S = $matches[4]; 33 | $this->V = $matches[5]; 34 | $this->a = $matches[6]; 35 | return $this; 36 | } 37 | 38 | protected function fromRGBaMatches($matches) 39 | { 40 | $R = $matches[3]/255; 41 | $G = $matches[4]/255; 42 | $B = $matches[5]/255; 43 | 44 | $min = min($R, $G, $B); 45 | $max = max($R, $G, $B); 46 | $delta = $max - $min; 47 | 48 | $this->V = $max; 49 | 50 | if($delta == 0) { 51 | $this->H = 0; 52 | $this->S = 0; 53 | } else { 54 | $this->S = $delta / $max; 55 | $dr = ((($max - $R) / 6) + ($delta / 2)) / $delta; 56 | $dg = ((($max - $G) / 6) + ($delta / 2)) / $delta; 57 | $db = ((($max - $B) / 6) + ($delta / 2)) / $delta; 58 | 59 | if ($R === $max) { 60 | $this->H = $db - $dg; 61 | } elseif ($G === $max) { 62 | $this->H = (1 / 3) + $dr - $db; 63 | } elseif ($B === $max) { 64 | $this->H = (2 / 3) + $dg - $dr; 65 | } 66 | 67 | if ($this->H < 0) { 68 | $this->H += 1; 69 | } else if ($this->H > 1) { 70 | $this->H -= 1; 71 | } 72 | } 73 | 74 | $this->H = round($this->H * 360); 75 | $this->S = round($this->S * 100); 76 | $this->V = round($this->V * 100); 77 | 78 | if(isset($matches[6]) && strlen($matches[6] > 0)) { 79 | $this->a = $matches[6]; 80 | } else { 81 | $this->a = 1; 82 | } 83 | return $this; 84 | } 85 | 86 | protected function fromHEXaMatches($matches) 87 | { 88 | $parts = str_split($matches[1], 2); 89 | $RGBaMatches = [ 90 | 3 => hexdec($parts[0]), 91 | 4 => hexdec($parts[1]), 92 | 5 => hexdec($parts[2]), 93 | 6 => 1, 94 | ]; 95 | if(count($parts) == 4) { 96 | $RGBaMatches[6] = hexdec($parts['3'])/255; 97 | } 98 | $this->fromRGBaMatches($RGBaMatches); 99 | return $this; 100 | } 101 | 102 | public function __toString() 103 | { 104 | return sprintf(self::FORMAT, $this->H, $this->S, $this->V, $this->a); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /lib/ColorConverter/Color/RGB.php: -------------------------------------------------------------------------------- 1 | R, $this->G, $this->B, $this->a); 12 | } 13 | } -------------------------------------------------------------------------------- /lib/ColorConverter/Color/RGBa.php: -------------------------------------------------------------------------------- 1 | H / 360) * 6; 20 | $s = $HSVa->S / 100; 21 | $v = $HSVa->V / 100; 22 | 23 | $i = floor($h); 24 | 25 | $f = $h - $i; 26 | $p = $v * (1 - $s); 27 | $q = $v * (1 - $f * $s); 28 | $t = $v * (1 - (1 - $f) * $s); 29 | 30 | $mod = $i % 6; 31 | $r = [$v, $q, $p, $p, $t, $v][$mod]; 32 | $g = [$t, $v, $v, $q, $p, $p][$mod]; 33 | $b = [$p, $p, $t, $v, $v, $q][$mod]; 34 | 35 | $this->R = round($r * 255); 36 | $this->G = round($g * 255); 37 | $this->B = round($b * 255); 38 | $this->a = $HSVa->a; 39 | 40 | return $this; 41 | } 42 | 43 | public function __toString() 44 | { 45 | return sprintf(self::FORMAT, $this->R, $this->G, $this->B, $this->a); 46 | } 47 | } -------------------------------------------------------------------------------- /lib/ColorConverter/ColorConverter.php: -------------------------------------------------------------------------------- 1 | isInputValid($input, self::INPUT_HSVA) 36 | || $this->isInputValid($input, self::INPUT_RGBA) 37 | || $this->isInputValid($input, self::INPUT_HEXA); 38 | } 39 | 40 | /** 41 | * @param $string 42 | * @return HSVa 43 | */ 44 | public function convertStringToHSVa($string) 45 | { 46 | $matches = []; 47 | 48 | if(preg_match(self::INPUT_HSVA, $string, $matches)) { 49 | return HSVa::getFromHSVaMatches($matches); 50 | } elseif(preg_match(self::INPUT_RGBA, $string, $matches)) { 51 | return HSVa::getFromRGBaMatches($matches); 52 | } elseif(preg_match(self::INPUT_HEXA, $string, $matches)) { 53 | return HSVa::getFromHEXaMatches($matches); 54 | } 55 | return new HSVa(); 56 | } 57 | 58 | /** 59 | * @param HSVa $HSVa 60 | * @return HEXa 61 | */ 62 | public function convertHSVaToHEXa(HSVa $HSVa) 63 | { 64 | $RGBa = $this->convertHSVaToRGBa($HSVa); 65 | return (new HEXa())->fromRGBa($RGBa); 66 | } 67 | 68 | /** 69 | * @param HSVa $HSVa 70 | * @return RGBa 71 | */ 72 | public function convertHSVaToRGBa(HSVa $HSVa) 73 | { 74 | return (new RGBa())->fromHSVa($HSVa); 75 | } 76 | 77 | /** 78 | * @param HSVa $HSVa 79 | * @return HEX 80 | */ 81 | public function convertHSVaToHEX(HSVa $HSVa) 82 | { 83 | $RGBa = $this->convertHSVaToRGBa($HSVa); 84 | return (new HEX())->fromRGBa($RGBa); 85 | } 86 | 87 | /** 88 | * @param HSVa $HSVa 89 | * @return RGB 90 | */ 91 | public function convertHSVaToRGB(HSVa $HSVa) 92 | { 93 | return (new RGB())->fromHSVa($HSVa); 94 | } 95 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "@babel/preset-react": "^7.0.0", 4 | "@simonwep/pickr": "^1.7.4", 5 | "@symfony/webpack-encore": "^0.28.0", 6 | "node-sass": "^4.11.0", 7 | "sass-loader": "^7.0.1", 8 | "webpack-notifier": "^1.8.0" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests/bundle 5 | tests/lib 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /tests/bundle/DependencyInjection/eZColorPickerExtensionTest.php: -------------------------------------------------------------------------------- 1 | extension = new eZColorPickerExtension(); 20 | 21 | parent::setUp(); 22 | } 23 | 24 | protected function getContainerExtensions(): array 25 | { 26 | return [$this->extension]; 27 | } 28 | 29 | public function testPrependEzpublish() 30 | { 31 | $this->load([]); 32 | 33 | $actualPrependedConfig = $this->container->getExtensionConfig('ezpublish'); 34 | // merge multiple configs returned 35 | $actualPrependedConfig = array_merge(...$actualPrependedConfig); 36 | 37 | $expectedPrependedConfig = [ 38 | 'admin_group' => [ 39 | 'field_templates' => [ 40 | [ 41 | 'template' => '@eZColorPicker/admin/content_fields.html.twig', 42 | 'priority' => 1, 43 | ] 44 | ] 45 | ], 46 | 'default' => [ 47 | 'field_templates' => [ 48 | [ 49 | 'template' => '@eZColorPicker/standard/content_fields.html.twig', 50 | 'priority' => 1, 51 | ], 52 | ] 53 | ], 54 | ]; 55 | 56 | self::assertSame( 57 | $expectedPrependedConfig, 58 | $actualPrependedConfig['system'] 59 | ); 60 | } 61 | 62 | public function testPrependTwig() 63 | { 64 | $this->load([]); 65 | 66 | $actualPrependedConfig = $this->container->getExtensionConfig('twig'); 67 | // merge multiple configs returned 68 | $actualPrependedConfig = array_merge(...$actualPrependedConfig); 69 | 70 | $expectedPrependedConfig = [ 71 | 'form_themes' => [ 72 | "@eZColorPicker/admin/field_template.html.twig" 73 | ] 74 | ]; 75 | 76 | self::assertSame( 77 | $expectedPrependedConfig, 78 | $actualPrependedConfig 79 | ); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/bundle/FieldType/ColorPicker/TypeTest.php: -------------------------------------------------------------------------------- 1 | getMockBuilder(ValueSerializerInterface::class)->getMock(); 39 | $validatorMock = $this->getMockBuilder(ValidatorInterface::class)->getMock(); 40 | $this->instance = new ColorPickerType($serializerMock, $validatorMock); 41 | } else { 42 | /** Version 2.5 */ 43 | $this->instance = new ColorPickerType(); 44 | } 45 | 46 | $this->instance->setColorConverter(new ColorConverter()); 47 | $this->emptyValue = new ColorPickerValue(); 48 | $this->filledValue = (new ColorPickerValue())->setValueFromHash(ColorPickerDataFixtures::ARRAY_VALUE); 49 | $this->filledValueNoAlpha = (new ColorPickerValue())->setValueFromHash(ColorPickerDataFixtures::ARRAY_VALUE_NO_ALPHA); 50 | } 51 | 52 | public function testEmptyValue() 53 | { 54 | $this->assertTrue($this->instance->isEmptyValue($this->emptyValue)); 55 | $this->assertFalse($this->instance->isEmptyValue($this->filledValue)); 56 | } 57 | 58 | public function testAcceptValueNull() 59 | { 60 | $this->assertEquals($this->emptyValue, $this->instance->acceptValue(null)); 61 | } 62 | 63 | public function testAcceptValueArray() 64 | { 65 | $this->assertEquals($this->filledValue, $this->instance->acceptValue(ColorPickerDataFixtures::ARRAY_VALUE)); 66 | } 67 | 68 | public function testAcceptValueString() 69 | { 70 | foreach ([ColorPickerDataFixtures::VALUE_HSVa, ColorPickerDataFixtures::VALUE_HEXa, ColorPickerDataFixtures::VALUE_RGBa] as $value) { 71 | $this->assertEquals($this->filledValue, $this->instance->acceptValue($value), 'Input value is '.$value); 72 | } 73 | 74 | foreach ([ColorPickerDataFixtures::VALUE_HEX, ColorPickerDataFixtures::VALUE_RGB] as $value) { 75 | $this->assertEquals($this->filledValueNoAlpha, $this->instance->acceptValue($value), 'Input value is '.$value); 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /tests/bundle/FieldType/ColorPicker/ValueTest.php: -------------------------------------------------------------------------------- 1 | value = new ColorPickerValue(); 23 | $this->data = ColorPickerDataFixtures::ARRAY_VALUE; 24 | } 25 | 26 | public function testIsEmpty() 27 | { 28 | $this->assertTrue($this->value->isEmpty()); 29 | } 30 | 31 | public function testHashFunctions() 32 | { 33 | $this->value->setValueFromHash($this->data); 34 | $this->assertFalse($this->value->isEmpty()); 35 | 36 | $hash = $this->value->getValueAsHash(); 37 | foreach ($this->data as $key => $value) { 38 | $this->assertArrayHasKey($key, $hash); 39 | $this->assertEquals($value, $hash[$key]); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /tests/lib/ColorConverter/ColorConverterTest.php: -------------------------------------------------------------------------------- 1 | instance = new ColorConverter(); 24 | $this->HSVa = $this->instance->convertStringToHSVa(ColorPickerDataFixtures::VALUE_HSVa); 25 | 26 | } 27 | 28 | public function testInputHSVa() 29 | { 30 | $this->assertTrue($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HSVa, ColorConverter::INPUT_HSVA)); 31 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HSVa, ColorConverter::INPUT_RGBA)); 32 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HSVa, ColorConverter::INPUT_HEXA)); 33 | } 34 | 35 | public function testInputRGBa() 36 | { 37 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGBa, ColorConverter::INPUT_HSVA)); 38 | $this->assertTrue($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGBa, ColorConverter::INPUT_RGBA)); 39 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGBa, ColorConverter::INPUT_HEXA)); 40 | } 41 | 42 | public function testInputHEXa() 43 | { 44 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEXa, ColorConverter::INPUT_HSVA)); 45 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEXa, ColorConverter::INPUT_RGBA)); 46 | $this->assertTrue($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEXa, ColorConverter::INPUT_HEXA)); 47 | } 48 | 49 | public function testInputRGB() 50 | { 51 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGB, ColorConverter::INPUT_HSVA)); 52 | $this->assertTrue($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGB, ColorConverter::INPUT_RGBA)); 53 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_RGB, ColorConverter::INPUT_HEXA)); 54 | } 55 | 56 | public function testInputHEX() 57 | { 58 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEX, ColorConverter::INPUT_HSVA)); 59 | $this->assertFalse($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEX, ColorConverter::INPUT_RGBA)); 60 | $this->assertTrue($this->instance->isInputValid(ColorPickerDataFixtures::VALUE_HEX, ColorConverter::INPUT_HEXA)); 61 | } 62 | 63 | public function testConvertStringToHSVa() 64 | { 65 | $values = [ 66 | ColorPickerDataFixtures::VALUE_HSVa, 67 | ColorPickerDataFixtures::VALUE_RGBa, 68 | ColorPickerDataFixtures::VALUE_HEXa, 69 | ]; 70 | foreach ($values as $value) { 71 | $this->assertEquals(ColorPickerDataFixtures::VALUE_HSVa, 72 | (string)$this->instance->convertStringToHSVa($value), 73 | 'Input string is '.$value 74 | ); 75 | } 76 | $values = [ 77 | ColorPickerDataFixtures::VALUE_RGB, 78 | ColorPickerDataFixtures::VALUE_HEX, 79 | ]; 80 | foreach ($values as $value) { 81 | $this->assertEquals(ColorPickerDataFixtures::VALUE_HSV, 82 | (string)$this->instance->convertStringToHSVa($value), 83 | 'Input string is '.$value 84 | ); 85 | } 86 | } 87 | 88 | public function testConvertHSVa() { 89 | $this->assertEquals(ColorPickerDataFixtures::VALUE_RGBa, $this->instance->convertHSVaToRGBa($this->HSVa)); 90 | $this->assertEquals(ColorPickerDataFixtures::VALUE_HEXa, $this->instance->convertHSVaToHEXa($this->HSVa)); 91 | $this->assertEquals(ColorPickerDataFixtures::VALUE_RGB, $this->instance->convertHSVaToRGB($this->HSVa)); 92 | $this->assertEquals(ColorPickerDataFixtures::VALUE_HEX, $this->instance->convertHSVaToHEX($this->HSVa)); 93 | } 94 | 95 | public function testLimitValues() { 96 | $this->assertEquals('rgba(255, 204, 0, 1.00)', (string)$this->instance->convertHSVaToRGBa( 97 | $this->instance->convertStringToHSVa('#FFCC00FF') 98 | )); 99 | $this->assertEquals('rgba(255, 255, 0, 1.00)', (string)$this->instance->convertHSVaToRGBa( 100 | $this->instance->convertStringToHSVa('#FFFF00FF') 101 | )); 102 | $this->assertEquals('rgba(255, 255, 204, 1.00)', (string)$this->instance->convertHSVaToRGBa( 103 | $this->instance->convertStringToHSVa('#FFFFCCFF') 104 | )); 105 | } 106 | } -------------------------------------------------------------------------------- /tests/lib/ColorConverter/ColorPickerDataFixtures.php: -------------------------------------------------------------------------------- 1 | self::VALUE_RGBa, 18 | 'HEXa' => self::VALUE_HEXa, 19 | 'HSVa' => self::VALUE_HSVa, 20 | 'RGB' => self::VALUE_RGB, 21 | 'HEX' => self::VALUE_HEX, 22 | ]; 23 | 24 | const ARRAY_VALUE_NO_ALPHA = [ 25 | 'RGBa' => 'rgba(176, 25, 25, 1.00)', 26 | 'HEXa' => '#B01919FF', 27 | 'HSVa' => self::VALUE_HSV, 28 | 'RGB' => self::VALUE_RGB, 29 | 'HEX' => self::VALUE_HEX, 30 | ]; 31 | } --------------------------------------------------------------------------------