Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Option to disable automatic conversion between HEX3and HEX6 colors #325

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/docs/examples/Basics.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,36 @@
{{/content}}
{{/extend}}

{{#extend "example"}}
{{#content "title"}} Disable HEX change from #RGB to #RRGGBB {{/content}}
{{#content "description"}}
<p>
By default, if the <code>autoInputFallback</code> option is enabled, even when you will
change input to HEX3 (#RGB), it will automatically convert to hex6 (#RRGGBB)
<br>
You can stop this by setting this option to false. That way, when someone will type
color to input, that is identified as HEX color, it will convert to #RRGGBB only if it will
be a valid HEX6 color.
</p>
{{/content}}
{{#content "code"}}
<div id="cp14" class="input-group">
<input type="text" class="form-control input-lg" value="I am an invalid color"/>
<span class="input-group-append">
<span class="input-group-text colorpicker-input-addon"><i></i></span>
</span>
</div>
<script>
$(function () {
$('#cp14').colorpicker({
autoInputFallback: false,
autoHexInputFallback: false,
});
});
</script>
{{/content}}
{{/extend}}

{{#extend "example"}}
{{#content "title"}} Adjust the popover options{{/content}}
{{#content "description"}}
Expand All @@ -374,15 +404,15 @@
</p>
{{/content}}
{{#content "code"}}
<div id="cp14" class="input-group">
<div id="cp15" class="input-group">
<input type="text" class="form-control input-lg" value="hsl(103, 70%, 64%)"/>
<span class="input-group-append">
<span class="input-group-text colorpicker-input-addon"><i></i></span>
</span>
</div>
<script>
$(function () {
$('#cp14').colorpicker({
$('#cp15').colorpicker({
popover: {
title: 'Adjust the color',
placement: 'top'
Expand Down Expand Up @@ -414,7 +444,7 @@
</button>
</div>
<div class="modal-body">
<input id="cp15" type="text" class="form-control input-lg" value="rgb(130, 44, 29)"/>
<input id="cp16" type="text" class="form-control input-lg" value="rgb(130, 44, 29)"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
Expand All @@ -425,7 +455,7 @@
</div>
<script>
$(function () {
$('#cp15').colorpicker();
$('#cp16').colorpicker();
});
</script>
{{/content}}
Expand Down
7 changes: 5 additions & 2 deletions src/js/ColorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ class ColorHandler {
* @fires Colorpicker#colorpickerInvalid
* @param {*} val
* @param {boolean} fallbackOnInvalid
* @param {boolean} autoHexInputFallback
* @returns {ColorItem}
*/
createColor(val, fallbackOnInvalid = true) {
let color = new ColorItem(this.resolveColorDelegate(val), this.format);
createColor(val, fallbackOnInvalid = true, autoHexInputFallback = false) {
let disableHexInputFallback = !fallbackOnInvalid && !autoHexInputFallback;

let color = new ColorItem(this.resolveColorDelegate(val), this.format, disableHexInputFallback);

if (!color.isValid()) {
if (fallbackOnInvalid) {
Expand Down
17 changes: 12 additions & 5 deletions src/js/ColorItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ class ColorItem {
/**
* @param {ColorItem|HSVAColor|QixColor|String|*|null} color Color data
* @param {String|null} format Color model to convert to by default. Supported: 'rgb', 'hsl', 'hex'.
* @param {boolean} disableHexInputFallback Disable fixing hex3 format
*/
constructor(color = null, format = null) {
this.replace(color, format);
constructor(color = null, format = null, disableHexInputFallback = false) {
this.replace(color, format, disableHexInputFallback);
}

/**
Expand All @@ -95,10 +96,11 @@ class ColorItem {
*
* @param {ColorItem|HSVAColor|QixColor|String|*|null} color Color data to be parsed (if needed)
* @param {String|null} format Color model to convert to by default. Supported: 'rgb', 'hsl', 'hex'.
* @param {boolean} disableHexInputFallback Disable fixing hex3 format
* @example color.replace('rgb(255,0,0)', 'hsl');
* @example color.replace(hsvaColorData);
*/
replace(color, format = null) {
replace(color, format = null, disableHexInputFallback = false) {
format = ColorItem.sanitizeFormat(format);

/**
Expand All @@ -114,7 +116,7 @@ class ColorItem {
* @type {QixColor}
* @private
*/
this._color = ColorItem.parse(color);
this._color = ColorItem.parse(color, disableHexInputFallback);

if (this._color === null) {
this._color = QixColor();
Expand All @@ -135,11 +137,12 @@ class ColorItem {
* parsed.
*
* @param {ColorItem|HSVAColor|QixColor|String|*|null} color Color data
* @param {boolean} disableHexInputFallback Disable fixing hex3 format
* @example let qColor = ColorItem.parse('rgb(255,0,0)');
* @static
* @returns {QixColor|null}
*/
static parse(color) {
static parse(color, disableHexInputFallback = false) {
if (color instanceof QixColor) {
return color;
}
Expand All @@ -164,6 +167,10 @@ class ColorItem {
format = 'hsv';
}

if (ColorItem.isHex(color) && (color.length !== 6 && color.length !== 7) && disableHexInputFallback) {
return null;
}

try {
return QixColor(color, format);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/Colorpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class Colorpicker {
return;
}

ch.color = val ? ch.createColor(val, this.options.autoInputFallback) : null;
ch.color = val ? ch.createColor(val, this.options.autoInputFallback, this.options.autoHexInputFallback) : null;

/**
* (Colorpicker) When the color is set programmatically with setValue().
Expand Down
12 changes: 12 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ export default {
* @default true
*/
autoInputFallback: true,
/**
* If true, valid HEX3 colors will be converted to HEX6, even with
* autoInputFallback set to false
* if false, HEX3 colors will not be converted to HEX6, when autoInputFallback is false
* (this has been an issue, when using HEX6 colors with
* autoInputFallback set to false, HEX3 colors were
* automatically converting to HEX6)
*
* @type {boolean}
* @default false
*/
autoHexInputFallback: true,
/**
* If true a hash will be prepended to hexadecimal colors.
* If false, the hash will be removed.
Expand Down