Stylesheet Development with PTC ALD > Adding PTC ALD Code to Stylesheet Source > Samples > Testing > Test Existing Text Properties and Apply Formatting Properties > Set Text Color Shade based on Text Color
  
Set Text Color Shade based on Text Color
The sample listed here describes how to test the current text color for a paragraph and vary its shade for a piece of inline text in that paragraph according to the current value of an attribute set for the inline text.
Refer to the sample file Arbortext-path/samples/APP/Testing/TextProperties/testTextProperties.xml. The paragraphs in the chapter Emphasis testing the text colour each have a role attribute. The value of that attribute can be red, blue, green, or magenta. Conditions in the testTextProperties.style that are based on these attribute values set the font size property for the para in chapter context:
para role="red"para in chapter context in stylesheet, condition IF attribute “role”=”red” — Font color #FF0000 (red)
para role="blue"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”blue” — Font color #0000FF (blue)
para role="green"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”green” — Font color #00FF00 (green)
para role="magenta"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”magenta” — Font color #FF00FF (magenta)
Inline text in a paragraph is tagged as emphasis, with the role attribute having one of two values set, either role=”light” or role=“dark“. The conditions based on the role attribute reference one of two property sets, depending on its value:
The condition If attribute "role" = "light" for the emphasis everywhere context in the stylesheet references the property set testTextColourLighter. The property set tests the current text color in operation in the paragraph, set by the role attribute on the paragraph, and applies a lighter shade (100/50) of that color to the piece of inline text.
The condition If attribute "role" = "dark" for the emphasis everywhere context in the stylesheet references the property set testTextColourDarker. The property set tests the current text color in operation in the paragraph, and applies a darker shade (50/100) of that color to the piece of inline text.
The relevant code in the property set is given below (note that this code fragment is from the testTextColourLighter property set):
var currentColour = new fColor(formatting.currentStyle.color);

switch (currentColour.toString()) {
//Red
case "{100,0,0}":
style.color="100/50 red";
break;
//Blue
case "{0,0,100}":
style.color="100/50 blue";
break;
//Green
case "{0,100,0}":
style.color="100/50 green";
break;
//Magenta
case "{100,0,100}":
style.color="100/50 magenta";
break;
}
Extract the current text color for the paragraph
var currentColour = new fColor(formatting.currentStyle.color);
Create the variable currentColour to hold the current text color. You reference this variable later, when setting the shade of the text color for the current element.
Create a new fColor object via the fColor() method of the fColor object. The value that will be returned for the new fColor object is the value of the color property of the current style. The current style is defined in the currentStyle property of the fFormatting object.
Pass the new fColor object, with its associated value, into the currentColour variable.
Set the shade of the text color for the current element based on the current text color
switch (currentColour.toString()) {
//Red
case "{100,0,0}":
style.color="100/50 red";
break;
//Blue
case "{0,0,100}":
style.color="100/50 blue";
break;
//Green
case "{0,100,0}":
style.color="100/50 green";
break;
//Magenta
case "{100,0,100}":
style.color="100/50 magenta";
break;
}
Use a switch statement to create a list of possible options (cases) for processing the value of the currentColour variable.
Note the toString() entry for currentColour. The toString() method for an fColor object returns the color as a text string. For example, if the actual value of currentColour is #0000FF, it will be returned as blue.
As the content of currentHeight is an fLength object, you can use this associated method to process its value.
The switch statement works in three stages:
1. The value of the variable currentColour is evaluated.
2. The value of currentColour is compared with the values for each case entry.
3. If the value of currentColour matches a case entry in the switch statement, the block of code associated with that case is executed.
An entry of break prevents one case from running into the next
So if the text height for a paragraph is blue, case "{0,0,100}" will match. The code phrase style.color="100/50 blue"; will be executed. The first part of the phrase style.color specifies that you want to provide a value for the color property for the style of the context, which is based on an fStyle object. The second half ="100/50 blue" confirms that the value of the color property should be a 50% shade of blue. The content of the current element will be output in 50% blue text.
In this sample, the current element is the emphasis element, with the condition If attribute "role" = "light". This context wraps a piece of inline text in a paragraph, so inline text will be output in 50% blue text if the current text color for the paragraph in which it appears is blue. Refer to the para in chapter context in the stylesheet and the condition IF attribute "role"="light". You can see that the font color for this context is set to #0000FF (blue).