-
Notifications
You must be signed in to change notification settings - Fork 7.6k
#7723 - Adding Support For '0x' Notation Color Format #13154
Conversation
Hi @nyteksf, if the issue is that tinycolor doesn't support function asRGB(color) {
if (/0x/.test(color)) {
return color.replace("0x", "#");
}
return color;
}
/** somewhere down the line where tinycolor is called **/
tinycolor(asRGB(this._getColor())
// when and only when you need the actual color value (for example presentation) convert it to 0x format:
if (this._format === "0x") {
return color.toHexString().replace("#", "0x");
}
return color.toHexString(); |
var that = this; | ||
//MUST CONVERT 0X TO HEX6 NOTATION AND BACK IN ORDER TO BE ABLE TO USE COLOR W/ TINYCOLOR(COLOR). TINYCOLOR DOESN'T SUPPORT 0x NOTATION! | ||
function checkIf0xNotation(color,that){ | ||
if((/0x/).test(color)){ //Is input in 0x Notation? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should really be /^0x/.test(color)
function checkIf0xNotation(color,that){ | ||
if((/0x/).test(color)){ //Is input in 0x Notation? | ||
//IF YES, THEN CHANGE/UPDATE SETTINGS: | ||
var thatColor = color.replace("0x","#"); //CONVERT 0x TO HEX6, ie 0xFFAACC => #FFAACC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of thatColor
use something more appropriate, like hexColor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't look that bad, as @petetnt said, define helper functions like isColor0xNotation
, color0xToHex
, colorHexTo0x
and use them in your code
@nyteksf if TinyColor doesn't support the 0x notation, I think you should file an issue to https://github.com/bgrins/TinyColor to ask support for it. |
|
||
function checkSetFormat(color,toStr){ | ||
if((/^0x/).test(color)){ | ||
var colorRes = color0xToHex(color,toStr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not create a variable if it's used only once, you can do return color0xToHex(color,toStr)
directly
if((/^0x/).test(color)){ | ||
var colorRes = color0xToHex(color,toStr); | ||
return colorRes; | ||
}else{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when if
branch ends with a return statement, else branch is not required and introduces unnecessary nesting, write
if (condition) {
return something;
}
return somethingelse;
instead of
if (condition) {
return something;
} else {
return somethingelse;
}
hexColor._format = "0x"; | ||
|
||
if(toStr !== null){ | ||
hexColor.toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this do?
I'd expect toString();
result to be assigned to something?
or did you mean to do return hexColor.toString();
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return hexColor.toString()
is correct.
@@ -328,7 +367,8 @@ define(function (require, exports, module) { | |||
// TinyColor actually generates to see if it's different. If so, then we assume the color | |||
// was incomplete to begin with. | |||
if (newColorOk) { | |||
newColorOk = (newColorObj.toString() === this._normalizeColorString(newColor)); | |||
var colorStr = newColor.replace("0x","#") || newColor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this newColor.replace("0x","#") || newColor
should be wrapped in a function with a proper name like normalizeColorString
or something which hints, why we're doing this
@@ -363,10 +403,12 @@ define(function (require, exports, module) { | |||
|
|||
// Create swatches | |||
swatches.forEach(function (swatch) { | |||
var swatchValue = swatch.value.replace("0x","#") || swatch.value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, wrap this in some sort of function
|
||
if((/^0x/).test(previewCSS)){ | ||
previewCSS = previewCSS.replace("0x","#"); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ending bracket }
seems to be wrongly indented
@nyteksf added a few comments, please have a look |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of simple comments, looking good so far! Keep up the good work!
if((/^0x/).test(color)){ | ||
var colorRes = color0xToHex(color,toStr); | ||
return colorRes; | ||
}else{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space between } else {
} | ||
|
||
function checkSetFormat(color,toStr){ | ||
if((/^0x/).test(color)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: spaces: if ((/^0x/).test(color)) {
* Convert 0x notation into hex6 format for tinycolor | ||
* compatibility: ("0xFFAACC" => "#FFFFFF") | ||
*/ | ||
function color0xToHex(color,toStr){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan of the second parameter, maybe rename it to something like convertToString
and make it a boolean 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha! That's the idea behind what I was going for. Thanks. :)
var hexColor = tinycolor(format0xToHexColor); | ||
hexColor._format = "0x"; | ||
|
||
if(toStr !== null){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nits: Can be written as if (!toStr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You meant if (toStr)
:)
var previewCSS = gradientMatch.prefix + (gradientMatch.colorValue || match[0]); | ||
var previewCSS = gradientMatch.prefix + (gradientMatch.colorValue || match[0]); | ||
|
||
if((/^0x/).test(previewCSS)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: spaces if ((/^0x/).test(previewCSS)) {
, misaligned bracket on line 407
this._updateColorTypeRadioButtons(colorObject.getFormat()); | ||
this.$colorValue.val(colorValue); | ||
this.$currentColor.css("background-color", colorValue); | ||
this.$currentColor.css("background-color", checkSetFormat(colorValue,"toStr")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the comment about the second paremeter above
Okay. Firstly, I just want to take a moment to thank you guys, once again, for helping me--each one of these feedback sessions has been worth at least a million articles or book pages by dint of contrast. That said, in response to my most recent changes, I think I have satisfied your associated change requests. Or no? Please let me know if otherwise, and again, I will hop to that so I can get onto the next one. Good times. :) |
var previewCSS = gradientMatch.prefix + (gradientMatch.colorValue || match[0]); | ||
var previewCSS = gradientMatch.prefix + (gradientMatch.colorValue || match[0]); | ||
|
||
if ((/^0x/).test(previewCSS)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be a function like:
function ensureHexFormat(str) {
return (/^0x/).test(str) ? str.replace("0x","#") : str;
}
adding constructs like these without explanation makes the code less readable to me
@@ -302,6 +339,10 @@ define(function (require, exports, module) { | |||
ColorEditor.prototype._normalizeColorString = function (color) { | |||
var normalizedColor = color; | |||
|
|||
// Convert from 0x notation into hex format string | |||
if (color.match(/^0x[0-9a-fA-F]{6}/)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can re-use the ensureHexFormat
function I mentioned in the comment before here?
Just two of my comments, lets wait for @petetnt to re-check this too. |
…ternalized code fragment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of small nits from me + @zaggino's comments, otherwise LGTM. We need to add some tests for this in another commit (and check that the relevant tests still pass)
var hexColor = tinycolor(format0xToHexColor); | ||
hexColor._format = "0x"; | ||
|
||
if(convertToStr){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more nit: if (convertToString) {
(we really should enable http://eslint.org/docs/rules/space-after-keywords and similar in our config, our spaces are wildly inconsistent across the codebase 🤔 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Convert 0x notation into hex6 format for tinycolor | ||
* compatibility: ("0xFFAACC" => "#FFFFFF") | ||
*/ | ||
function color0xToHex(color,convertToStr){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space after comma color, convertToStr
return color.toHexString().replace("#","0x"); | ||
} | ||
|
||
function checkSetFormat(color,convertToStr){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space after comma color, convertToStr
#7723 - Removed stray comment, restored proper indentation of function
@@ -325,6 +325,11 @@ define(function (require, exports, module) { | |||
function hasLengthInPixels(args) { | |||
return (args.length > 1 && args[1].indexOf("px") > 0); | |||
} | |||
|
|||
// Ensures that input is in usable hex format | |||
function ensureHexFormat(str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nyteksf
wrong indentation, do you have brackets-eslint
extension installed? it should have given you a warning here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops. That said, it passed my tests. And I had thought I had brackets-eslint installed, but I'm not sure anymore. Is that not the same as what would be found when running the command grunt eslint
? I ask as it seems npm just downloaded something. If not, then it should be taken care of from here on out, at least.
Looks good now except one place where the code is incorrectly indented, it'd be a good thing to install |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some missing JSDoc strings, otherwise LGTM if tests pass.
:) |
Merged, thanks @nyteksf |
Great job @nyteksf! Thanks for this contribution! |
Hello. Could somebody please point me in the right direction from here where I am stuck? I have implemented the RegExp used to find "0x..." format colors which are entered as input. I have also added the associated button, and plugged in the related code that it might be used (for the most part). However, I'm now having trouble linking everything up in a non-hackish way (or at all) in large part because tinycolor doesn't recognize 0x notation. As a result I cannot get the color swatches to work, or 0x notation to be easily recognized by the given methods of tinycolor like .getFormat() which is for example used with a switch case in Brackets for control flow. Thanks in advance to anyone who can lend me their support! :)
@zaggino