001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.colour;
031
032import java.awt.Color;
033import java.lang.reflect.Field;
034import java.util.ArrayList;
035import java.util.List;
036
037/**
038 * Convenience constants and methods for RGB colours for use in MBFImages
039 * 
040 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
041 * 
042 */
043public class RGBColour {
044        /** White colour as RGB */
045        public final static Float[] WHITE = { 1f, 1f, 1f };
046
047        /** Light gray colour as RGB */
048        public final static Float[] LIGHT_GRAY = { 0.75f, 0.75f, 0.75f };
049
050        /** Gray colour as RGB */
051        public final static Float[] GRAY = { 0.5f, 0.5f, 0.5f };
052
053        /** Dark gray colour as RGB */
054        public final static Float[] DARK_GRAY = { 0.25f, 0.25f, 0.25f };
055
056        /** Black colour as RGB */
057        public final static Float[] BLACK = { 0f, 0f, 0f };
058
059        /** Red colour as RGB */
060        public final static Float[] RED = { 1f, 0f, 0f };
061
062        /** Pink colour as RGB */
063        public final static Float[] PINK = { 1f, 175f / 256f, 175f / 256f };
064
065        /** Orange colour as RGB */
066        public final static Float[] ORANGE = { 1f, 200f / 256f, 0f };
067
068        /** Yellow colour as RGB */
069        public final static Float[] YELLOW = { 1f, 1f, 0f };
070
071        /** Green colour as RGB */
072        public final static Float[] GREEN = { 0f, 1f, 0f };
073
074        /** Magenta colour as RGB */
075        public final static Float[] MAGENTA = { 1f, 0f, 1f };
076
077        /** Cyan colour as RGB */
078        public final static Float[] CYAN = { 0f, 1f, 1f };
079
080        /** Blue colour as RGB */
081        public final static Float[] BLUE = { 0f, 0f, 1f };
082
083        private RGBColour() {
084        }
085
086        /**
087         * Make an OpenImaj colour from a java.awt.Color.
088         * 
089         * @param c
090         *            the color to convert
091         * @return a colour for using as RGB MBFImages
092         */
093        public static Float[] fromColor(Color c) {
094                final Float r = c.getRed() / 255f;
095                final Float g = c.getRed() / 255f;
096                final Float b = c.getRed() / 255f;
097
098                return new Float[] { r, g, b };
099        }
100
101        /**
102         * Get a colour from a string representation. The string may contain any of
103         * the predefined colour names described in this class (in upper, lower or
104         * mixed case), or it can be a comma separated triplet of floating point
105         * values.
106         * 
107         * @param s
108         *            the string
109         * @return the colour described by the string, or BLACK if a problem occurs.
110         */
111        public static Float[] fromString(String s) {
112                try {
113                        if (s.contains(",")) {
114                                final String[] parts = s.split(",");
115                                final Float[] col = new Float[3];
116                                for (int i = 0; i < 3; i++) {
117                                        col[i] = Float.parseFloat(parts[i].trim());
118                                }
119                                return col;
120                        } else {
121                                final Field f = RGBColour.class.getField(s.toUpperCase());
122                                return (Float[]) f.get(null);
123                        }
124                } catch (final Exception e) {
125                        return RGBColour.BLACK;
126                }
127        }
128
129        /**
130         * Generate a random colour
131         * 
132         * @return a random colour
133         */
134        public static Float[] randomColour() {
135                final Float[] c = new Float[3];
136
137                c[0] = (float) Math.random();
138                c[1] = (float) Math.random();
139                c[2] = (float) Math.random();
140
141                return c;
142        }
143
144        /**
145         * Generate a list of random colours.
146         * 
147         * @param n
148         *            number of colours
149         * @return list of randomly generated colours
150         */
151        public static List<Float[]> randomColours(int n) {
152                final List<Float[]> colours = new ArrayList<Float[]>();
153
154                for (int i = 0; i < n; i++)
155                        colours.add(randomColour());
156
157                return colours;
158        }
159
160        /**
161         * Generate a range of colours from a {@link ColourMap}.
162         * 
163         * @param cm
164         *            the map
165         * @param n
166         *            number of colours
167         * @return the colours
168         */
169        public static Float[][] coloursFromMap(ColourMap cm, int n) {
170                final Float[][] cols = new Float[n][];
171                for (int i = 0; i < n; i++) {
172                        final float frac = (float) i / (float) n;
173                        cols[i] = cm.apply(frac);
174                }
175                return cols;
176        }
177
178        /**
179         * Create a colour from an RGB triplet with integer values in the range
180         * 0..255.
181         * 
182         * @param r
183         *            red value 0..255
184         * @param g
185         *            green value 0..255
186         * @param b
187         *            blue value 0..255
188         * @return the colour value
189         */
190        public static Float[] RGB(int r, int g, int b) {
191                return new Float[] { r / 255f, g / 255f, b / 255f };
192        }
193
194        /**
195         * Create a colour from an RGB triplet with float values in the range 0..1.
196         * 
197         * @param r
198         *            red value 0..1
199         * @param g
200         *            green value 0..1
201         * @param b
202         *            blue value 0..1
203         * @return the colour value
204         */
205        public static Float[] RGB(float r, float g, float b) {
206                return new Float[] { r, g, b };
207        }
208}