import java.io.*; import java.lang.*; import java.awt.*; /** * Colour stores a RGB triple and supports several operations to * manipulate colour values * * @author Anthony Steed * @version 1.0 */ public class Colour { /** * Holds the red component of the colour */ public double R; /** * Holds the green component of the colour */ public double G; /** * Holds the blue component of the colour */ public double B; /** * Create a new colour and set it to the default colour (black) */ public Colour () { reset(); } /** * Create a new colour with the given red green and blue values * @param r the red component * @param g the green component * @param b the blue component */ public Colour (double r, double g, double b) { R = r; G = g; B = b; } /** * Create a new colour by copying an existing colour * @param c the colour to copy */ public Colour(Colour c) { R = c.R; G = c.G; B = c.B; } /** * Copying an existing colour to this colour * @param c the colour to copy */ public void copy(Colour c) { R = c.R; G = c.G; B = c.B; } /** * Reset the colour to the default colour (black) */ public void reset () { R = G = B = 0.0; } /** * Clamp each component of the colour to lie within the range 0.0 * to 1.0 */ public void clamp() { if (R < 0.0) R = 0.0; if (G < 0.0) G = 0.0; if (B < 0.0) B = 0.0; if (R > 1.0) R = 1.0; if (G > 1.0) G = 1.0; if (B > 1.0) B = 1.0; } /** * Add the given colour to this colour * @param c the colour to add */ public void add (Colour c) { R += c.R; G += c.G; B += c.B; } /** * Test if the colour is black * @return whether the colour is 0,0,0 */ public boolean isBlack () { return ((R==0) && (G==0) && (B==0)); } /** * Static implementation of colour add function that creates a new * colour object from the result. * @param c1 the first colour to add * @param c2 the second colour to add * @return a new colour object with the results */ public static Colour add (Colour c1, Colour c2) { return new Colour (c1.R + c2.R, c1.G + c2.G, c1.B + c2.B); } /** * Static implementation of colour add function that write the result * of the addition to a given colour object. * @param cd the colour to write the result to * @param c1 the first colour to add * @param c2 the second colour to add */ public static void add (Colour cd, Colour c1, Colour c2) { cd.R = c1.R + c2.R; cd.G = c1.G + c2.G; cd.B = c1.B + c2.B; } /** * Multiply the given colour to this colour * @param c the colour to multiply */ public void mult (Colour c) { R *= c.R; G *= c.G; B *= c.B; } /** * Static implementation of colour mult function that creates a * new colour object from the result. * @param c1 the first colour to add * @param c2 the second colour to add * @return a new colour object with the results */ public static Colour mult (Colour c1, Colour c2) { return new Colour (c1.R * c2.R, c1.G * c2.G, c1.B * c2.B); } /** * Static implementation of colour mult function that write the result * of the multiplication to a given colour object. * @param cd the colour to write the result to * @param c1 the first colour to multiply * @param c2 the second colour to multiply */ public static void mult (Colour cd, Colour c1, Colour c2) { cd.R = c1.R * c2.R; cd.G = c1.G * c2.G; cd.B = c1.B * c2.B; } /** * Scale this colour by the given factor * @param s the scale factor */ public void scale (double s) { R *= s; G *= s; B *= s; } /** * Static scale function that returns a new colour scaled by the * given factor * @param c1 the colour to scale * @param s the scale factor */ public static Colour scale (Colour c1, double s) { return new Colour (c1.R * s, c1.G * s, c1.B * s); } /** * Static scale function that writes the colour scaled by the * given factor to a given destination * @param cd the destination colour * @param c1 the colour to scale * @param s the scale factor */ public static void scale (Colour cd, Colour c1, double s) { cd.R = c1.R * s; cd.G = c1.G * s; cd.B = c1.B * s; } /** * Create a standard java Color object from this * colour for rendering purposes. * given factor to a given destination * @return a new Color object equivalent to the current RGB values */ public Color getColor() { return new Color((float)R,(float)G,(float)B); } /** * Read the colour from the given source * @param is the source to read from * @exception java.io.IOException * if the light can not be read * @exception java.io.NumberFormatException * if there a number format error is encountered */ public void read(SceneReader is) throws IOException, NumberFormatException { R = is.readDouble(); G = is.readDouble(); B = is.readDouble(); clamp(); } /** * Write the colour to the given destination * @param os the destination to write to * @exception java.io.IOException * if the write fails. */ public void write(SceneWriter os) throws IOException { os.writeDouble(R); os.writeChar(' '); os.writeDouble(G); os.writeChar(' '); os.writeDouble(B); os.writeChar(' '); } /** * Print a human readable version of the colour definition to the * given destination * @param os the destination to write to * @exception java.io.IOException * if the write fails. */ public void print(SceneWriter os) throws IOException { os.writeChar('('); os.writeDouble(R); os.writeChar(','); os.writeDouble(G); os.writeChar(','); os.writeDouble(B); os.writeChar(')'); } }