0

In javaScript (within Photoshop) when giving a hex colour a value it's in quotes eg "FCAA00"

 var hexCol = "FCAA00";
 var fgColor = new SolidColor;
 fgColor.rgb.hexValue = hexCol

but when passing a variable to that value you don't need the quotes. Why is this?

 var hexCol = convertRgbToHex(252, 170, 0)
 hexCol = "\"" + hexCol + "\""; // These quotes are not needed.
 var fgColor = new SolidColor;
 fgColor.rgb.hexValue = hexCol

Is this just a javaScript quirk or am I missing something going on behind the scenes, as it were. Thank you.

3
  • What's going on in convert (I'm assuming not concert?)RgbToHex? Commented Oct 20, 2013 at 8:11
  • If convertRGBToHex() returns a string, then that is the equivalent of var hexCol = "FCAA00"; Commented Oct 20, 2013 at 8:20
  • concertRgbToHex() that's where all the the hipster numbers hang out and listen to music in an open air stadium. In hex... (Facepalm of typographic shame!)
    – Ghoul Fool
    Commented Oct 20, 2013 at 9:35

1 Answer 1

3

The quotation marks are a syntactic construct which denote a string literal. I.e. the parser knows that the characters between the quotations marks form the value of a string. That also means that they are not part of the value itself, they are only relevant for the parser.

Examples:

// string literal with value foo
"foo" 

// the string **value** is assigned to the variable bar, 
// i.e. the variables references a string with value foo
var bar = "foo"; 

// Here we have three strings:
// Two string literals with the value " (a quotation mark)
// One variable with the value foo
// The three string values are concatenated together and result in "foo",
// which is a different value than foo
var baz = "\"" + bar + "\"";

The last case is what you tried. It creates a string which literally contains quotation marks. It's equivalent to writing

"\"foo\""

which is clearly different than "foo".

1
  • 1
    Good answer. For Ghoul, I suggest thinking about what happened if you didn't use quotes on "FCAA00". The parser would look for a variable called FCAA00 and wouldn't find it. In fact, you could write some pretty confusing code like FCAA00 = "00AA00";
    – Jere
    Commented Oct 20, 2013 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.