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

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

Merged
merged 17 commits into from
Mar 14, 2017
Merged

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

merged 17 commits into from
Mar 14, 2017

Conversation

nyteksf
Copy link
Contributor

@nyteksf nyteksf commented Mar 7, 2017

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
Copy link
Collaborator

petetnt commented Mar 8, 2017

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

Copy link
Contributor

@zaggino zaggino left a 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

@ficristo
Copy link
Collaborator

ficristo commented Mar 9, 2017

@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)){
var colorRes = color0xToHex(color,toStr);
Copy link
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

if((/^0x/).test(color)){
var colorRes = color0xToHex(color,toStr);
return colorRes;
}else{
Copy link
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;
}

hexColor._format = "0x";

if(toStr !== null){
hexColor.toString();
Copy link
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
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.

@@ -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;
Copy link
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

@@ -363,10 +403,12 @@ define(function (require, exports, module) {

// Create swatches
swatches.forEach(function (swatch) {
var swatchValue = swatch.value.replace("0x","#") || swatch.value;
Copy link
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
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
Copy link
Contributor

zaggino commented Mar 12, 2017

@nyteksf added a few comments, please have a look

Copy link
Collaborator

@petetnt petetnt left a 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{
Copy link
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 {

}

function checkSetFormat(color,toStr){
if((/^0x/).test(color)){
Copy link
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")
*/
function color0xToHex(color,toStr){
Copy link
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
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 hexColor = tinycolor(format0xToHexColor);
hexColor._format = "0x";

if(toStr !== null){
Copy link
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
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
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
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
Copy link
Contributor Author

nyteksf commented Mar 13, 2017

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

@@ -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}/)) {
Copy link
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
Copy link
Contributor

zaggino commented Mar 13, 2017

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

Copy link
Collaborator

@petetnt petetnt left a 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){
Copy link
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
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")
*/
function color0xToHex(color,convertToStr){
Copy link
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

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

function checkSetFormat(color,convertToStr){
Copy link
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
Copy link
Contributor Author

nyteksf commented Mar 13, 2017

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
@@ -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) {
Copy link
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
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
Copy link
Contributor

zaggino commented Mar 13, 2017

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)

Copy link
Collaborator

@petetnt petetnt left a 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.

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 14, 2017

:)
Thanks!
@petetnt

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

zaggino commented Mar 14, 2017

Merged, thanks @nyteksf

@petetnt
Copy link
Collaborator

petetnt commented Mar 14, 2017

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