Wakefield's Daily Tips

Setting colours in code

In Python colours are simply defined by a list of three numbers, representing red, green and blue on a scale of -1 to +1.

Online you have to define a colour object using these three numbers. Builder components create these colour objects automatically, as do text.setColor(newColor) or polygon.setFillColor(newColor) in code components (where newColor can be either a colour name or a list of three numbers).

Polygon border colours can also be set via the component. On the other hand, if you want to set a polygon border colour in code then you need to change the JavaScript side from polygon.setLineColor(newColor) to polygon.setLineColor(new util.Color(newColor))

To avoid this manual translation, I create colour objects in a Both code component in my setup routine.

For example:

Python

white = "white"
grey = [0, 0, 0]
transparent = None;

JavaScript

white = new util.Color("white");
grey = new util.Color([0, 0, 0]);
transparent = null;

Then polygon.setLineColor(white) or text.LineColor(transparent) will work in an Auto translated code component. Do not use polygon.setBorderColor(newColor) since this will fail online.