tints

Support for creating tintable game components.
TintingFilter class
TintFilter() : AbstractTintingFilter constructor
TintOverlayFilter() : AbstractTintingFilter constructor
ReplaceFilter() constructor
Tintable
Tintable.setTint( hue, saturation, brightness )
Tintable.getTint()
TintCache( filter ) constructor
TintCache.setImage( image )
TintCache.setFactors( hue, saturation, brightness )
TintCache.getTintedImage()

Support for creating tintable game components.

Tintable components provide an easy way for users to introduce new variants of basic designs. Tintable components provide one or more tint panels, controls that allow the user to select a colour. The colour(s) selected are then used to recolour selected parts of a component design. For example, it might be applied to a decorative border running around the outside. This library provides the image processing tools needed to apply tinting options to graphics while painting a game component; the uicontrols and uibindings libraries provide the controls and logic needed to support tinting on the user interface side.

Tinting modified the original image by adusting its hue, saturation, and brightness. Hue refers to the colour of the image pixels. The change in hue is expressed as an anglular distance around a colour wheel. (So, shifting the hue by 180 degrees would change the colour to its complementary colour, on the opposite side of the colour wheel.) Saturation describes the purity of the image pixels. The lower the saturation, the more grayish and washed out the colour appears. A saturation of 0 results in a shade of grey; a saturation of 1 results in the purest possible expression of the hue. The saturation adjustment for tinting is expressed as a scaling factor, a multiplier that is applied to the original value. Brightness describes how light or dark the image appears. A brightness of 0 results in a completely black image. A brightness of 1 results in the brightest possible colour within the limitations of the hue and saturation. Like saturation, the brightness adjustment for tinting is expressed as a scaling factor.

TintingFilter class

This interface is implemented by all image filters that can be used with a TintCache (see below). For details about this interface, refer to its API documentation.

TintFilter() : AbstractTintingFilter constructor

This filter shifts the hue angle and scales the saturation and brightness of every pixel in an image. The image's alpha (opacity) channel is not affected, so translucent areas remain translucent.

The hue angle is measured using a scale in which 1 is equal to 360 degrees (a full circle). Values will normally range from -0.5 to +0.5 (-180 to +180 degrees), but values outside of this range are acceptable. A value of 0 leaves the original hue unchanged.

The saturation and brightness values are factors multiplied against the saturation and brightness of the source pixel. Factors less than 0 are treated as 0. Factors may be more than 1 (100%). If the scaled value for a given pixel is more than 1, it is clamped at 1 in the result.

If using an HSBPanel, it will always provide a hue between -0.5 and +0.5, and saturation and brightness values between 0 and 1. Depending on the saturation and brightness of the source image, you may wish to scale those values up or down before passing them to the TintCache. Otherwise, using the panel it will only be possible to decrease the brightness and saturation (or keep it the same). For example, if you wanted the maximum brightness scale to be 1.2 (120%), multiply the value returned from the HSBPanel by 1.2. When choosing the default brightness, use 1/1.2 if you want the default settings to work out to the original image (1/1.2 * 1.2 = 1).

For convenience, the class TintFilter.ScaledTintFilter can perform scaling for you:

 // scale saturation to 120%, brightness to 200%
 var f = new TintFilter.ScaledTintFilter( h, s, b, 1.2, 2.0 );
A ScaledTintFilter gets and sets factors between 0 and 1 as usual, but internally applies the requested scaling factors.

TintOverlayFilter() : AbstractTintingFilter constructor

This filter sets the entire source image to the same HSB colour value (without affecting transparency).

ReplaceFilter() constructor

This filter replaces the hue and saturation, and scales the brightness, of the pixels in the source image. This filter can be used to tint a greyscale (black and white) source image. Depending on the brightness of the source image, you may wish to scale the brightness value passed to the filter up in order to cover a wider range of possible tints. (For example, if the average brightness of the source image is 50%, you might scale the brightness value by 2.)

Tintable

Tintable is a Java interface that is implemented by objects that can act as models for an HSBPanel. Panels read their initial settings by calling the Tintable's getTint method, and call its setTint method when the tint controls are adjusted. By implementing the Tintable interface you can process messages from the control panel yourself. Normally, however, you do not need to implement this interface. A standard implementation is provided through the uibindings library that can read and write tints to a private setting on a component.

Tintable.setTint( hue, saturation, brightness )

Called to set the tint being managed by this Tintable.

Tintable.getTint()

Returns an array of three floats that represent the current hue, saturation, and brightness values of the tint being managed by this Tintable.

TintCache( filter ) constructor

A TintCache improves drawing performance when working with tints. It takes advantage of the fact that the base image to be tinted doesn't usually change between draws, and that the user only rarely adjusts the tint compared to other editing operations. Instead of applying a tint filter to draw the tinted graphic each time the card is redrawn, you can create a cache, set the filter and base image to use, and then request the tinted version of the image as needed. The cache object will keep a tinted copy of the image available, and update it as needed when the selected tint changes.

To create a new tint cache, you must pass in a new instance of the tinting filter class that you wish to use (typically an instance of TintFilter).

For example:

 // during setup:
 var frontTinter = new TintCache( new TintFilter() );
 frontTinter.setImage( frontImage );

 // ...

 // during drawing:
 frontTinter.setFactors( hueShift, saturationScale, brightnessScale );
 var tintedImage = frontTinter.getTintedImage();

TintCache.setImage( image )

Sets the source image that will have tinting applied to it.
image a BufferedImage that will be tinted using the tinting filter

TintCache.setFactors( hue, saturation, brightness )

Set the HSB tinting factors to use when tinting the source image.
hue the hue adjustment; the exact effect depends on the filter used
saturation the saturation adjustment; the exact effect depends on the filter used
brightness the brightness adjustment; the exact effect depends on the filter used

TintCache.getTintedImage()

Returns a tinted image that is created by applying the tinting filter used to construct this cache to the current source image, using the current HSB factors. If the image and factors have not changed since the last call, this may return a cached result.

If the current source image data has been modified by writing to the image (for example, if you used as the destination image for some other filtering operation), then you should force any cached result to be cleared before calling this method. An example:

 var tc = new TintCache( new TintFilter() );
 tc.setFactors( -0.25, 1, 0.8 );
 tc.setImage( source );

 // ...

 var tinted = TintCache.getTintedImage();

 var g = source.createGraphics();
 try {
     // ...
     // modify the pixels in "source"
     // ...
 } finally {
     g.dispose();
 }

 // force clearing cached results before getting tinted version
 // of the modified source image:
 tc.setImage( null );
 tc.setImage( source );
 tinted = TintCache.getTintedImage();

returns a tinted version of the source image

Contents