Here you can find the source of colorText(int v, int vlo, int vhi, int rlo, int glo, int blo, int rhi, int ghi, int bhi)
Parameter | Description |
---|---|
v | The value to be shown. Between vlo and vhi. Extreme value taken if it is out of range vlo or vhi. |
vlo | The Lower bound of value |
vhi | The Upper bound of value |
rlo | The value of red component when v is at its lower bound, in range 0 to 255 |
rhi | The value of red component when v is at its upper bound, in range 0 to 255 |
glo | The value of green component when v is at its lower bound, in range 0 to 255 |
ghi | The value of green component when v is at its upper bound, in range 0 to 255 |
blo | The value of blue component when v is at its lower bound, in range 0 to 255 |
bhi | The value of blue component when v is at its upper bound, in range 0 to 255 |
public static String colorText(int v, int vlo, int vhi, int rlo, int glo, int blo, int rhi, int ghi, int bhi)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w . j a v a 2 s. c o m * Color a text using BBCode such that the color either increase or decrease one value to * other value for each component of the color * A gradient would be produced from vlo to vhi, as v varies. * @param v The value to be shown. Between vlo and vhi. Extreme value taken if it is out of range vlo or vhi. * @param vlo The Lower bound of value * @param vhi The Upper bound of value * @param rlo The value of red component when v is at its lower bound, in range 0 to 255 * @param rhi The value of red component when v is at its upper bound, in range 0 to 255 * @param glo The value of green component when v is at its lower bound, in range 0 to 255 * @param ghi The value of green component when v is at its upper bound, in range 0 to 255 * @param blo The value of blue component when v is at its lower bound, in range 0 to 255 * @param bhi The value of blue component when v is at its upper bound, in range 0 to 255 * @return BBCoded string showing the text in generated color */ public static String colorText(int v, int vlo, int vhi, int rlo, int glo, int blo, int rhi, int ghi, int bhi) { boolean rInc = true; boolean gInc = true; boolean bInc = true; int rSmall = rlo; int gSmall = glo; int bSmall = blo; if (rhi < rlo) { rInc = false; rSmall = rhi; } if (ghi < glo) { gInc = false; gSmall = ghi; } if (bhi < blo) { bInc = false; bSmall = bhi; } double vRatio = (v + 0.0) / Math.abs(vhi - vlo); int r = (int) (rInc ? vRatio * Math.abs(rhi - rlo) + rSmall : (1 - vRatio) * Math.abs(rhi - rlo) + rSmall); int g = (int) (gInc ? vRatio * Math.abs(ghi - glo) + gSmall : (1 - vRatio) * Math.abs(ghi - glo) + gSmall); int b = (int) (bInc ? vRatio * Math.abs(bhi - blo) + bSmall : (1 - vRatio) * Math.abs(bhi - blo) + bSmall); String code = colorCode(r, g, b); return "[color=" + code + "]" + v + "[/color]"; } /** * Convert a color with its RGB value to HTML ready code * @param r Red component of color, in range 0 to 255 * @param g Green component of color, in range 0 to 255 * @param b Blue component of color, in range 0 to 255 * @return String of color which is HTML ready */ public static String colorCode(int r, int g, int b) { r = inRange(r, 0, 255); g = inRange(g, 0, 255); b = inRange(b, 0, 255); String rx = Integer.toHexString(r); String gx = Integer.toHexString(g); String bx = Integer.toHexString(b); if (rx.length() == 1) { rx = '0' + rx; } if (gx.length() == 1) { gx = '0' + gx; } if (bx.length() == 1) { bx = '0' + bx; } return '#' + rx + gx + bx; } /** * Avoid a number getting out of a certain range * @param n The number to be checked * @param lo The Lower margin of the number. n cannot be smaller than this * @param hi The Upper margin of the number. n cannot be bigger than this * @return If n is out of range, set to defined extreme values, otherwise return n */ public static int inRange(int n, int lo, int hi) { return n < lo ? lo : (n > hi ? hi : n); } /** * Avoid a number getting out of a certain range * @param n The number to be checked * @param lo The Lower margin of the number. n cannot be smaller than this * @param hi The Upper margin of the number. n cannot be bigger than this * @return If n is out of range, set to defined extreme values, otherwise return n */ public static double inRange(double n, double lo, double hi) { return n < lo ? lo : (n > hi ? hi : n); } }