Color Calculator:
A Java Hacker's Worksheet Sample

This is a Hacker's Worksheet that is configured to read a document where different variations are computed for Color values. The document being read is here, and the source code for this version of the applet is here. This is an illustration of using the Hacker's worksheet both with a document that contains the calculation logic, and as a JTable derivative: in this version, I've included custom renderers and editors in the various cells to make the information presentation more compelling. The only code in this applet is related to the custom renders and editors, because the spreadsheet document format does not allow for assigning renderers and editors (yet).

There are three cells that are editable, and a slider is provided to allow interaction with them (an identical slider is also used to render the cells, so the appearance doesn't change when the focus leaves the cell). As the slider is manipulated, the value of the cell is changed. The first color cell in the fourth column contains the formula:

    new java.awt.Color(cell("RedInput"),cell("GreenInput"),cell("BlueInput"))
... which computes a new color value when any of the input cells is modified. This cell is named "BaseColor". The two cells immediately below this compute two related colors: the darker() color and the brighter() color, using the formulas
-- and --
The first two rows of cells below the sliders contain the red, green, and blue values of the darker (or brighter) value in the last colum. So, when the slider is moved, the corresponding value change causes the base color cell to compute a new value, which triggers the darker and brigher cells to recompute, which causes the six individual color component cells to recompute.

The last row computes the nearest color in the standard browser palatte by rounding each of the three individual color components to the nearest multiple of 51, using the formula

    (cell("RedInput") + 26) / 51 * 51
... (or "GreenInput" or "BlueInput", obviously). The final value in the last row computes a new color based on the rounded data.



Below is a default worksheet reading the exact same document. The only difference is that there are no renderers or editors configured below: you may type invalid data (out of range, etc) and see the dependant cells show the error. Note that the default format for all objects is based on the value's toString() method.