Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

#7723 - Adding Support For '0x' Notation Color Format - #13154

Merged
zaggino merged 17 commits into
adobe:masterfrom
nyteksf:nyteksf/issue7723
Mar 14, 2017
Merged

zaggino merged 17 commits into
adobe:masterfrom
nyteksf:nyteksf/issue7723

Conversation

@nyteksf

@nyteksf nyteksf commented Mar 7, 2017 •

Copy link
Copy Markdown
Contributor

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

@petetnt

petetnt commented Mar 8, 2017

Copy link
Copy Markdown
Collaborator

Hi @nyteksf,

if the issue is that tinycolor doesn't support 0x format, I would suggest that you always convert any potential 0x values to RGB first. Basically kinda like you have at your checkIf0xNotation, but much more simpler. Something like:

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?

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

@zaggino zaggino left a comment

Copy link
Copy Markdown
Contributor

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

@ficristo

ficristo commented Mar 9, 2017

Copy link
Copy Markdown
Collaborator

@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.
(From what I saw newer version of the library still don't support the 0x notation, but you could double check)
ATM that repo doesn't seem very active so maybe nothing will come out anytime soon, but if they aren't aware of the issue it will never be fixed.

}

function checkSetFormat(color,toStr){
if((/^0x/).test(color)){

Copy link
Copy Markdown
Contributor

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

function checkSetFormat(color,toStr){
if((/^0x/).test(color)){
var colorRes = color0xToHex(color,toStr);
return colorRes;

Copy link
Copy Markdown
Contributor

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;
}

var hexColor = tinycolor(format0xToHexColor);
hexColor._format = "0x";

if(toStr !== null){

Copy link
Copy Markdown
Contributor

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(); ?

Copy link
Copy Markdown
Collaborator

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.

// was incomplete to begin with.
if (newColorOk) {
newColorOk = (newColorObj.toString() === this._normalizeColorString(newColor));
var colorStr = newColor.replace("0x","#") || newColor;

Copy link
Copy Markdown
Contributor

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


// Create swatches
swatches.forEach(function (swatch) {
var swatchValue = swatch.value.replace("0x","#") || swatch.value;

Copy link
Copy Markdown
Contributor

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","#");
};

Copy link
Copy Markdown
Contributor

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

@zaggino

zaggino commented Mar 12, 2017

Copy link
Copy Markdown
Contributor

@nyteksf added a few comments, please have a look

@petetnt petetnt left a comment

Copy link
Copy Markdown
Collaborator

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!

function checkSetFormat(color,toStr){
if((/^0x/).test(color)){
var colorRes = color0xToHex(color,toStr);
return colorRes;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space between } else {

return color.toHexString().replace("#","0x");
}

function checkSetFormat(color,toStr){

Copy link
Copy Markdown
Collaborator

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")
*/

Copy link
Copy Markdown
Collaborator

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 🤔

Copy link
Copy Markdown
Contributor Author

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 format0xToHexColor = color.replace("0x","#");
var hexColor = tinycolor(format0xToHexColor);
hexColor._format = "0x";

Copy link
Copy Markdown
Collaborator

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)

Copy link
Copy Markdown
Contributor

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)){

Copy link
Copy Markdown
Collaborator

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"));

Copy link
Copy Markdown
Collaborator

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

@nyteksf

nyteksf commented Mar 13, 2017 •

Copy link
Copy Markdown
Contributor Author

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)) {

Copy link
Copy Markdown
Contributor

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

var normalizedColor = color;

// Convert from 0x notation into hex format string
if (color.match(/^0x[0-9a-fA-F]{6}/)) {

Copy link
Copy Markdown
Contributor

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?

@zaggino

zaggino commented Mar 13, 2017

Copy link
Copy Markdown
Contributor

Just two of my comments, lets wait for @petetnt to re-check this too.

@petetnt petetnt left a comment

Copy link
Copy Markdown
Collaborator

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 format0xToHexColor = color.replace("0x","#");
var hexColor = tinycolor(format0xToHexColor);
hexColor._format = "0x";

Copy link
Copy Markdown
Collaborator

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 🤔 )

Copy link
Copy Markdown
Contributor

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")
*/

Copy link
Copy Markdown
Collaborator

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

function as0xString(color){
return color.toHexString().replace("#","0x");
}

Copy link
Copy Markdown
Collaborator

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

@nyteksf

nyteksf commented Mar 13, 2017

Copy link
Copy Markdown
Contributor Author

Okay. The latest change requests have been fulfilled, or so I believe. As always--please don't hesitate to let me know if there're any more in mind. Otherwise, thanks again. And see you again soon. :)

@zaggino @petetnt

nyteksf and others added 2 commits March 13, 2017 05:54
#7723 - Removed stray comment, restored proper indentation of function
}

// Ensures that input is in usable hex format
function ensureHexFormat(str) {

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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.

@zaggino

zaggino commented Mar 13, 2017

Copy link
Copy Markdown
Contributor

Looks good now except one place where the code is incorrectly indented, it'd be a good thing to install brackets-eslint extension and fix the warnings in the code you've modified (you don't need to fix warnings in code you didn't touch of course, we know there're some, but we're trying to get the codebase to be more consistent)

@petetnt petetnt left a comment

Copy link
Copy Markdown
Collaborator

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.

@nyteksf

nyteksf commented Mar 14, 2017 •

Copy link
Copy Markdown
Contributor Author

:)
Thanks!
@petetnt

@zaggino
zaggino merged commit 7372631 into adobe:master Mar 14, 2017
@zaggino

zaggino commented Mar 14, 2017

Copy link
Copy Markdown
Contributor

Merged, thanks @nyteksf

@petetnt

petetnt commented Mar 14, 2017

Copy link
Copy Markdown
Collaborator

Great job @nyteksf! Thanks for this contribution!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants