Colors in Ejs View Elements
A static color (i.e. a color that will not change during the execution of the simulation) for a given view element property is specified in Ejs using either one of the predefined Ejs colors:
BLACK | BLUE | CYAN | DARKGRAY |
GRAY | GREEN | LIGHTGRAY | MAGENTA |
ORANGE | PINK | RED | WHITE |
YELLOW |
or providing its integer RGB coordinates (between 0 and 255), plus an optional transparency coordinate, as in 0,253,12,255. Just type the desired value in the corresponding view element property. A specialized color chooser can also help you choose a given static color.
To specify a color that changes in run-time, you’ll need to declare a variable of type Object, associate it to the corresponding element property, and then change the variable according to your program’s logic.
For instance, if you declare a variable in Ejs called myColor
, of type Object, the lines:
myColor = java.awt.Color.RED;
myColor = new java.awt.Color(255,0,0);
can both be used in the Java code of your program to create a solid red color in run-time. The associated view element property will change accodingly.
If you need semi-transparent colors, use something like:
myColor = new java.awt.Color(255,0,0,127);
The last parameter is the level of transparency desired. 255 makes a full solid color. Semi-transparent colors may slow down a little bit your program.
Finally, some elements (specially the drawables) accept an integer (int
) as color. This integer is passed to the org.opensourcephysics.display.DisplayColors.getLineColor(int)
method to produce a nice scale of colors for subsequent integers. This is the procedure used by the getLineMethod()
:
float h=((float)(index*Math.PI/12))%1; // each increment moves slightly more than 1/4 turn around color wheel
float s=1.0f; // maximum saturation for vibrant colors
float b=0.5f; // lines are often thin and should be darker for visibility on light backgrounds
return java.awt.Color.getHSBColor( h, s, b);