001 package org.nakedobjects.applib.value;
002
003 import org.nakedobjects.applib.annotation.Facets;
004
005
006 /**
007 * Color is simple numerical representation of a color using the brightness of red, green and blue (RGB)
008 * components.
009 *
010 * <p>
011 * Where there is no basic colors (RGB all equal 0) then you get black; where each color is at maximum (RGB
012 * all equal 255) you get white.
013 *
014 * <p>
015 * TODO: currently this value type still uses <tt>@Facets</tt> rather than <tt>@Value</tt>.
016 */
017 @Facets(facetFactoryNames = { "org.nakedobjects.metamodel.value.ColorValueTypeFacetFactory" })
018 public class Color extends Magnitude {
019 private static final long serialVersionUID = 1L;
020 public final static Color WHITE = new Color(0xffffff);
021 public final static Color BLACK = new Color(0);
022 private final int color;
023
024 public Color(final int color) {
025 this.color = color;
026 }
027
028 public int intValue() {
029 return color;
030 }
031
032
033 @Override
034 public boolean equals(final Object obj) {
035 if (obj instanceof Color) {
036 final Color c = (Color) obj;
037 return c.color == color;
038 }
039 return super.equals(obj);
040 }
041
042 /**
043 * returns true if the number of this object has the same value as the specified number
044 */
045 @Override
046 public boolean isEqualTo(final Magnitude number) {
047 if (number instanceof Color) {
048 return ((Color) number).color == color;
049 } else {
050 throw new IllegalArgumentException("Parameter must be of type Color");
051 }
052 }
053
054 /**
055 * Returns true if this value is less than the specified value.
056 */
057 @Override
058 public boolean isLessThan(final Magnitude value) {
059 if (value instanceof Color) {
060 return color < ((Color) value).color;
061 } else {
062 throw new IllegalArgumentException("Parameter must be of type Color");
063 }
064 }
065
066 public String title() {
067 if (color == 0) {
068 return "Black";
069 } else if (color == 0xffffff) {
070 return "White";
071 } else {
072 return "#" + Integer.toHexString(color).toUpperCase();
073 }
074 }
075
076 public String toString() {
077 return "Color: #" + Integer.toHexString(color).toUpperCase();
078 }
079 }
080 // Copyright (c) Naked Objects Group Ltd.