ColorInspector
- Responder
- View
- Inspector
- ColorInspector
- Inspector
- View
Click in the input field for the color. Choose a color. Press Enter or click outside the selector to validate the color you have chosen. The plate changes of color.
Directly enter a value, e.g. #CB6
.
The code which lets a user select a color uses jQuery MiniColors.
- function ColorInspector(value = ColorInspector.defaultColor) {
- if (!Validator.validateColor(value))
- throw new TypeError();
- Inspector.call(this);
- this._value = Validator.normalizeColor(value);
- }
- ColorInspector.prototype = Object.create(Inspector.prototype);
- Object.defineProperty(ColorInspector.prototype, 'constructor', { value: ColorInspector, enumerable: false, writable: true });
- ColorInspector.defaultColor = '#000000';
- ColorInspector.prototype.validate = function(val) {
- return Validator.validateColor(val);
- };
- ColorInspector.prototype.normalize = function(val) {
- return Validator.normalizeColor(val);
- };
- ColorInspector.prototype.resetWidget = function() {
- this._widget.value = this._value;
- jQuery(this._widget).minicolors('value', this._value);
- return this;
- };
- ColorInspector.prototype.manageWidget = function(parent = null) {
- Inspector.prototype.manageWidget.call(this, parent);
- if (this._parent && this._widget) {
- jQuery(this._widget).minicolors({
- letterCase: 'uppercase',
- defaultColor: ColorInspector.defaultColor,
- hide: function() {
- this.dispatchEvent(new Event('change'));
- }
- });
- }
- return this;
- };
- ColorInspector.prototype.unmanageWidget = function() {
- if (this._widget)
- jQuery(this._widget).minicolors('destroy');
- Inspector.prototype.unmanageWidget.call(this);
- return this;
- };
- ColorInspector.prototype.createWidget = function() {
- const htmlinput = `<input type="text" size="8" maxlength="7" value="${this._value}" spellcheck="false" />`;
- let template = document.createElement('template');
- template.innerHTML = htmlinput;
- let widget = template.content.children[0];
- this.setWidget(widget);
- return this;
- };
Test
- <?php $text='Objective.js'; ?>
- <?php $font='Slackey'; ?>
- <?php $fontSize=24; ?>
- <?php $color='#cccccc'; ?>
- <?php head('font', $font); ?>
- <?php head('javascript', 'jquery.minicolors'); ?>
- <?php head('stylesheet', 'jquery.minicolors', 'screen'); ?>
Adds the tags <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans%7CSlackey"/>
, <script src="/js/jquery.minicolors.js"/>
and <link rel="stylesheet" href="/css/jquery.minicolors.css" media="screen"/>
to the <head>
section of the HTML document to load the code and the style sheet of MiniColors in jQuery.
head
is a function of iZend.
Adapt the code to your development environment.
- <?php $id=uniqid('id'); ?>
- .test_display {
- width: 240px;
- height: 135px;
- display: flex;
- margin-bottom: 10px;
- border-radius: 3px;
- }
- <div id="<?php echo $id; ?>" class="noprint">
- <div class="test_display">
- <canvas width="240" height="135"></canvas>
- </div>
- <div class="ojs">
- <div>
- <span class="color_panel"></span>
- </div>
- </div>
- </div>
- <?php head('javascript', '/objectivejs/Objective.js'); ?>
- <?php head('javascript', '/objectivejs/Validator.js'); ?>
- <?php head('javascript', '/objectivejs/Responder.js'); ?>
- <?php head('javascript', '/objectivejs/View.js'); ?>
- <?php head('javascript', '/objectivejs/Inspector.js'); ?>
- <?php head('javascript', '/objectivejs/ColorInspector.js'); ?>
- function ColorView() {
- View.call(this);
- this._color = '<?php echo $color; ?>';
- this._canvas = null;
- }
- ColorView.prototype = Object.create(View.prototype);
- Object.defineProperty(ColorView.prototype, 'constructor', { value: ColorView, enumerable: false, writable: true });
- ColorView.prototype.setColor = function(color) {
- if (this._color != color) {
- this._color = color;
- if (this._widget)
- this._widget.style.backgroundColor = color;
- }
- return this;
- }
- ColorView.prototype.resetWidget = function() {
- if (this._widget)
- this._widget.style.backgroundColor = this._color;
- return this;
- }
- ColorView.prototype.setWidget = function(w) {
- const canvas = w.querySelector('canvas');
- if (canvas === null)
- throw new TypeError();
- this._canvas = canvas;
- View.prototype.setWidget.call(this, w);
- return this;
- }
- ColorView.prototype.drawWidget = function(font, fontSize, text) {
- if (this._canvas) {
- let canvas = this._canvas;
- let ctx = canvas.getContext('2d');
- ctx.font = `bold ${fontSize}px "${font}"`;
- ctx.fillStyle = 'white';
- ctx.textAlign = 'center';
- ctx.textBaseline = 'middle';
- ctx.fillText('', canvas.width / 2, canvas.height / 2);
- ctx.beginPath();
- ctx.arc(20, 20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(canvas.width-20, 20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(canvas.width-20, canvas.height-20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(20, canvas.height-20, 10, 0, 2 * Math.PI);
- ctx.fill();
- document.fonts.ready.then(() => {
- ctx.fillText(text, canvas.width / 2, canvas.height / 2);
- });
- }
- return this;
- }
- function Tester(display, inspector) {
- Responder.call(this);
- this._display = display;
- inspector.addNextResponder(this);
- this._inspector = inspector;
- }
- Tester.prototype = Object.create(Responder.prototype);
- Object.defineProperty(Tester.prototype, 'constructor', { value: Tester, enumerable: false, writable: true });
- Tester.prototype.inspectorValueChanged = function(sender) {
- if (sender === this._inspector)
- this._display.setColor(sender.get());
- return true;
- }
- const inspector = new ColorInspector('<?php echo $color; ?>');
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = new ColorView();
- display.setWidget(container.querySelector('.test_display')).resetWidget();
- display.drawWidget('<?php echo $font; ?>', <?php echo $fontSize; ?>, '<?php echo $text; ?>');
- inspector.createManagedWidget(container.querySelector('.color_panel'));
- const tester = new Tester(display, inspector);
SEE ALSO
Inspector, BooleanInspector, NumberInspector, StringInspector, SelectInspector, OptionInspector, SelectionInspector, ImageInspector, Editor, Validator
Comments