Repository navigation
#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.
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.
instead of thatColor use something more appropriate, like hexColor
|
@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)){ |
There was a problem hiding this comment.
do not create a variable if it's used only once, you can do return color0xToHex(color,toStr) directly
| function checkSetFormat(color,toStr){ | ||
| if((/^0x/).test(color)){ | ||
| var colorRes = color0xToHex(color,toStr); | ||
| return colorRes; |
There was a problem hiding this comment.
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;
}
| var hexColor = tinycolor(format0xToHexColor); | ||
| hexColor._format = "0x"; | ||
|
|
||
| if(toStr !== null){ |
There was a problem hiding this comment.
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.
return hexColor.toString() is correct.
| // 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.
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
|
|
||
| // Create swatches | ||
| swatches.forEach(function (swatch) { | ||
| var swatchValue = swatch.value.replace("0x","#") || swatch.value; |
There was a problem hiding this comment.
again, wrap this in some sort of function
|
|
||
| if((/^0x/).test(previewCSS)){ | ||
| previewCSS = previewCSS.replace("0x","#"); | ||
| }; |
There was a problem hiding this comment.
this ending bracket } seems to be wrongly indented
|
@nyteksf added a few comments, please have a look |
petetnt
left a comment
There was a problem hiding this comment.
Couple of simple comments, looking good so far! Keep up the good work!
| function checkSetFormat(color,toStr){ | ||
| if((/^0x/).test(color)){ | ||
| var colorRes = color0xToHex(color,toStr); | ||
| return colorRes; |
There was a problem hiding this comment.
Nit: space between } else {
| return color.toHexString().replace("#","0x"); | ||
| } | ||
|
|
||
| function checkSetFormat(color,toStr){ |
There was a problem hiding this comment.
Nit: spaces: if ((/^0x/).test(color)) {
| /** | ||
| * Convert 0x notation into hex6 format for tinycolor | ||
| * compatibility: ("0xFFAACC" => "#FFFFFF") | ||
| */ |
There was a problem hiding this comment.
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.
Aha! That's the idea behind what I was going for. Thanks. :)
| var format0xToHexColor = color.replace("0x","#"); | ||
| var hexColor = tinycolor(format0xToHexColor); | ||
| hexColor._format = "0x"; | ||
|
|
There was a problem hiding this comment.
Nits: Can be written as 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.
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.
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.
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
| 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.
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
| var format0xToHexColor = color.replace("0x","#"); | ||
| var hexColor = tinycolor(format0xToHexColor); | ||
| hexColor._format = "0x"; | ||
|
|
There was a problem hiding this comment.
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 🤔 )
| /** | ||
| * Convert 0x notation into hex6 format for tinycolor | ||
| * compatibility: ("0xFFAACC" => "#FFFFFF") | ||
| */ |
There was a problem hiding this comment.
Nit: space after comma color, convertToStr
| function as0xString(color){ | ||
| return color.toHexString().replace("#","0x"); | ||
| } | ||
|
|
There was a problem hiding this comment.
Nit: space after comma color, convertToStr
#7723 - Removed stray comment, restored proper indentation of function
| } | ||
|
|
||
| // Ensures that input is in usable hex format | ||
| function ensureHexFormat(str) { |
There was a problem hiding this comment.
@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.
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 |
petetnt
left a comment
There was a problem hiding this comment.
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