/** * This function will return a merged colour from low to high based on the number supplied. * From: http://code.calum.org/ * @param lowColour * @param highColour * @param percent * @return */ public static String getColour(String lowColour, String highColour, int percent){ if ( lowColour.length() != 6 ) return "Error with lowColour"; if ( highColour.length() != 6 ) return "Error with highColour"; if ( percent >= 100 ) return highColour; if ( percent <= 0 ) return lowColour; String lowRed = lowColour.substring(0, 2); String lowGreen = lowColour.substring(2, 4); String lowBlue = lowColour.substring(4, 6); String highRed = highColour.substring(0, 2); String highGreen = highColour.substring(2, 4); String highBlue = highColour.substring(4, 6); int lowR = hexStringToInt(lowRed); int lowG = hexStringToInt(lowGreen); int lowB = hexStringToInt(lowBlue); int highR = hexStringToInt(highRed); int highG = hexStringToInt(highGreen); int highB = hexStringToInt(highBlue); int r = -1; int g = -1; int b = -1; r = percentageBetween(lowR, highR, percent); g = percentageBetween(lowG, highG, percent); b = percentageBetween(lowB, highB, percent); String rStr = intToHexString(r); String gStr = intToHexString(g); String bStr = intToHexString(b); StringBuilder ret = new StringBuilder(); if ( rStr.length() == 1 ) { ret.append("0" + rStr); } else if ( rStr.length() == 2 ) { ret.append(rStr); } else { ret.append("rr"); } if ( gStr.length() == 1 ) { ret.append("0" + gStr); } else if ( gStr.length() == 2 ) { ret.append(gStr); } else { ret.append("gg"); } if ( bStr.length() == 1 ) { ret.append("0" + bStr); } else if ( bStr.length() == 2 ) { ret.append(bStr); } else { ret.append("xx"); } return ret.toString(); } /* * This function returns a value between lower and higher, based on percent. * 30, 70, 50 would return 50. * 20, 80, 25 would return 35. * @param lower the value of 0% * @param higher the value of 100% * @param percent the percentage. * @return * */ public static int percentageBetween(int lower, int higher, int percent) { int l = lower; int h = higher; int diff = Math.abs(h - l); float a = diff / 100F; float b = a * percent; int ret = -1; if ( l < h ) { ret = (int) (l + b); } else { ret = (int) (l - b); } return ret; } public static int hexStringToInt(String hex) { int i = Integer.valueOf(hex, 16).intValue(); return i; } public static String intToHexString(int i) { String hex = Integer.toHexString(i); return hex; }