X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FColorUtils.java;h=4ba17d53b80c1493db98f27aac3d77870f2c0ab8;hb=65d6d09cd08743d481d39521bb0298ea683888f8;hp=44ce0102f961659fe172c2594f38bcb79387c536;hpb=c7fc7690c14127c28c641ea3ff8a8193652fec17;p=jalview.git diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index 44ce010..4ba17d5 100644 --- a/src/jalview/util/ColorUtils.java +++ b/src/jalview/util/ColorUtils.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) - * Copyright (C) 2014 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -24,12 +24,18 @@ package jalview.util; +import jalview.api.ColorI; + import java.awt.Color; import java.util.Random; public class ColorUtils { + public static Color getColor(ColorI c) + { + return new Color(c.getRed(), c.getGreen(), c.getBlue()); + } /** * Generates a random color, will mix with input color. Code taken from * http://stackoverflow @@ -68,7 +74,7 @@ public class ColorUtils * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html# * tkcode */ - public static final String toTkCode(Color colour) + public static final String toTkCode(ColorI colour) { String colstring = "#" + ((colour.getRed() < 16) ? "0" : "") + Integer.toHexString(colour.getRed()) @@ -103,4 +109,43 @@ public class ColorUtils return col == null ? null : col.brighter().brighter().brighter(); } + /** + * Returns a color between minColour and maxColour; the RGB values are in + * proportion to where 'value' lies between minValue and maxValue + * + * @param value + * @param minValue + * @param minColour + * @param maxValue + * @param maxColour + * @return + */ + public static Color getGraduatedColour(float value, float minValue, + Color minColour, float maxValue, Color maxColour) + { + if (minValue == maxValue) + { + return minColour; + } + if (value < minValue) + { + value = minValue; + } + if (value > maxValue) + { + value = maxValue; + } + + /* + * prop = proportion of the way value is from minValue to maxValue + */ + float prop = (value - minValue) / (maxValue - minValue); + float r = minColour.getRed() + prop + * (maxColour.getRed() - minColour.getRed()); + float g = minColour.getGreen() + prop + * (maxColour.getGreen() - minColour.getGreen()); + float b = minColour.getBlue() + prop + * (maxColour.getBlue() - minColour.getBlue()); + return new Color(r / 255, g / 255, b / 255); + } }