Several immediate points stand out…
Creating the dictionary. You have created a used dictionary attribute $ColorDictionary. But you haven’t set it. This
$ColorDictionary=$ColorDictionary("Device RGB colorspace 1 1 0 1:yellow;Device RGB colorspace 1 0.5 0 1;orange ")
…is setting the entire value of $ColorDictionary for the note calling your function to the value of the same attribute whose $Name, improbably is "Device RGB colorspace 1 1 0 1:yellow;Device RGB colorspace 1 0.5 0 1;orange " (even down to the trailing space!). No matter, I think what you meant to use was (note the corrected ;
to :
in the second term):
$ColorDictionary=dictionary("Device RGB colorspace 1 1 0 1:yellow;Device RGB colorspace 1 0.5 0 1:orange ")
i.e. using the dictionary() action operator to make Dictionary-type from the supplied string—which of course is actually supplied as a list of value:key;
pairs.
So not the function populates $ColorDictionary of the note calling the function with two key:value pairs: Device RGB colorspace 1 1 0 1:yellow;
and Device RGB colorspace 1 0.5 0 1;orange
. Still not really helping.
We either want to store this dictionary in the $ColorDictionary of a single specific note or make a dynamic Dictionary-type variable inside the function and access values via function input, i.e. a look-up wrapper.
Using an attribute
I’ll assume we’ve a note called “_config” (i.e. at $Path /_config
) to hold the data. Where you put such a note and what you call it matters not except do make the name unique (thus my use of an underscore prefix), making it addressable via $Name alone. Obviously, if differing the title adjust example code below accordingly. We’ll make a Stamp “Set Colour Dictionary”, and assume it has all the key:value;
data needed:
$ColorDictionary=dictionary("Device RGB colorspace 1 1 0 1:yellow;Device RGB colorspace 1 0.5 0 1:orange ");
We use it to stamp our “_config” note. I see from the demo that data notes store the dictionary’s key values on their $HighlightColor:
So for the above note to correctly get the colour value of the key “Device RGB colorspace 1 0.5 0 1” it would call this code:
$MyString = $ColorDictionary("_config")["Device RGB colorspace 1 0.5 0 1"];
Tada!:
or, via the value of $HighlightColor:
$MyString = $ColorDictionary("_config")[$HighlightColor];
Using a function
We can employ a function() to store the dictionary. Here’s our function, commented to show how:
function fColorspaceLookup(iDevice){
// iDevice is a string input with a dictionary key value
// Define a Dictionary-type variable
var:dictionary vColorDict;
// store device-colorspace:color key:value pairs as needed
vColorDict.extend("Device RGB colorspace 1 1 0 1:yellow;");
vColorDict.extend("Device RGB colorspace 1 0.5 0 1:orange;");
// dictionary now complete
// return the colour value matching iDevice
return vColorDict[iDevice]
}
I know I’m adding key:value pairs one at a time but there may be many and they may change over time. this approach makes things more maintainable IMO. Feel free to write code more concisely if the lack of concision offends.
New we can use this code:
$MyString = fColorspaceLookup("Device RGB colorspace 1 0.5 0 1");
TaDa, again!:
or more realistically using $HighlightColor:
$MyString = fColorspaceLookup($HighlightColor);
So, which approach is better?
As we know there is no one right way! Use whichever appeals.
Here is your earlier file with my code added: (‘_config’ note, new function, stamps, and four test notes. The test notes are pairs. The first feeds the target dictionary or function a string, the second uses a the value of $HighlightColor.
Here: TestingColorDictionary.-ed.tbx (629.2 KB)
I think that covers it