001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on May 20, 2010
021 * Author: Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.gui.util.color;
026
027import java.awt.*;
028
029public class ColorUtils
030{
031
032        public static Color orange =   Color.decode("#FFA500");
033        public static Color cyan   =   Color.decode("#00FFFF");
034        public static Color gold   =   Color.decode("#FFD700");
035
036
037        static final Color c1 = Color.decode("#228B22"); // green
038        static final Color c2 = Color.decode("#8F8FFF"); // blue
039        static final Color c3 = gold;
040        static final Color c4 = Color.decode("#FF8C00"); // orange
041        static final Color c5 = Color.decode("#FF00FF"); // pink
042        static final Color c6 = Color.decode("#C71585"); // red
043
044        public static final Color[] colorWheel = new Color[] {c1, c2, c3, c4 , c5,c6};
045
046
047        public static void main(String[] args){
048                int i = -1;
049                for ( Color color : colorWheel){
050                        i++;
051                        float af[] = Color.RGBtoHSB(color.getRed(),color.getGreen(),color.getBlue(), null);
052                        System.out.println("position "  + i + "  " + af[0] + " " + af[1] + " " + af[2]);
053                        //System.out.println(rotateHue(color, 0.1f));
054                }
055        }
056
057        public static String toHexColor(Color col){
058                return Integer.toHexString((col.getRGB() & 0xffffff) | 0x1000000).substring(1);
059        }
060
061        /**
062         * Rotate a color through HSB space
063         * @param color Starting color
064         * @param fraction Amount to add to the hue. The integer part is discarded to leave a number in [0,1)
065         * @return
066         */
067        public static Color rotateHue (Color color, float fraction) {
068
069                float af[] = Color.RGBtoHSB(color.getRed(),color.getGreen(),color.getBlue(), null);
070
071                float hue = af[0];
072                float saturation = af[1];
073                float brightness = af[2];
074
075                float hueNew = hue  + fraction;
076
077                Color hsb = Color.getHSBColor(hueNew, saturation, brightness);
078                return new Color(hsb.getRed(), hsb.getGreen(), hsb.getBlue(), color.getAlpha());
079        }
080
081        public static Color getIntermediate(Color start, Color end, int stepSize, int position ){
082
083                float af1[] = Color.RGBtoHSB(start.getRed(),start.getGreen(),start.getBlue(), null);
084
085                float af2[] = Color.RGBtoHSB(end.getRed(),end.getGreen(),end.getBlue(), null);
086
087                float hue1 = af1[0];
088                float hue2 = af2[0];
089
090                if ( hue2 < hue1) {
091
092                        hue2 = af1[0];
093                        hue1 = af2[0];
094                }
095
096                //float saturation = af1[1] + af2[1] / 2f;
097                //float brightness = af1[2] + af2[2] / 2f;
098
099                float range = Math.abs(hue2-hue1);
100
101                while ( position > stepSize){
102                        position = position - stepSize ;
103                }
104
105                float inc = (range * position / stepSize) ;
106                float hueNew = hue1 + inc;
107
108                //System.out.println(position + " " + hue1 + " " + hue2 + " new: " + hueNew + " inc " + inc + " range " + range);
109                return Color.getHSBColor(hueNew, af1[1], af1[2]);
110
111        }
112
113        /**
114         * Make a color darker. (RGB color scheme)
115         *
116         * @param color     Color to make darker.
117         * @param fraction  Darkness fraction.
118         * @return          Darker color.
119         */
120        public static Color darker (Color color, double fraction)
121        {
122                int red   = (int) Math.round (color.getRed()   * (1.0 - fraction));
123                int green = (int) Math.round (color.getGreen() * (1.0 - fraction));
124                int blue  = (int) Math.round (color.getBlue()  * (1.0 - fraction));
125
126                if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
127                if (green < 0) green = 0; else if (green > 255) green = 255;
128                if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;
129
130                int alpha = color.getAlpha();
131
132                return new Color (red, green, blue, alpha);
133        }
134
135        /**
136         * Make a color lighter. (RGB color scheme)
137         *
138         * @param color     Color to make lighter.
139         * @param fraction  Darkness fraction.
140         * @return          Lighter color.
141         */
142        public static Color lighter (Color color, double fraction)
143        {
144                int red   = (int) Math.round (color.getRed()   * (1.0 + fraction));
145                int green = (int) Math.round (color.getGreen() * (1.0 + fraction));
146                int blue  = (int) Math.round (color.getBlue()  * (1.0 + fraction));
147
148                if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
149                if (green < 0) green = 0; else if (green > 255) green = 255;
150                if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;
151
152                int alpha = color.getAlpha();
153
154                return new Color (red, green, blue, alpha);
155        }
156
157}