0

I'm trying to get a random HSV colour then convert it to RGB to be used. Does anyone have any ideas as to how I can do this? Thanks, Sam. What I've got so far: First method converts HSV to RGB with the specified values, the second method is for getting a random colour.

public static float[] HSVtoRGB(float h, float s, float v) {
    float m, n, f;
    int i;

    float[] hsv = new float[3];
    float[] rgb = new float[3];

    hsv[0] = h;
    hsv[1] = s;
    hsv[2] = v;

    if (hsv[0] == -1) {
        rgb[0] = rgb[1] = rgb[2] = hsv[2];
        return rgb;
    }
    i = (int) (Math.floor(hsv[0]));
    f = hsv[0] - i;
    if (i % 2 == 0) {
        f = 1 - f; // if i is even
    }
    m = hsv[2] * (1 - hsv[1]);
    n = hsv[2] * (1 - hsv[1] * f);
    switch (i) {
        case 6:
        case 0:
            rgb[0] = hsv[2];
            rgb[1] = n;
            rgb[2] = m;
            break;
        case 1:
            rgb[0] = n;
            rgb[1] = hsv[2];
            rgb[2] = m;
            break;
        case 2:
            rgb[0] = m;
            rgb[1] = hsv[2];
            rgb[2] = n;
            break;
        case 3:
            rgb[0] = m;
            rgb[1] = n;
            rgb[2] = hsv[2];
            break;
        case 4:
            rgb[0] = n;
            rgb[1] = m;
            rgb[2] = hsv[2];
            break;
        case 5:
            rgb[0] = hsv[2];
            rgb[1] = m;
            rgb[2] = n;
            break;
    }
    return rgb;
}

public static int randomColor() {
    int hue = (int) (Math.random() * 6.0f);
    int saturation = (int) (Math.random());
    int brightness = (int) (Math.random());

    float[] rgb = HSVtoRGB(hue, saturation, brightness);

    int red = (int) (rgb[0] * 255.0f);
    int green = (int) (rgb[1] * 255.0f);
    int blue = (int) (rgb[2] * 255.0f);

    return (red << 16) | (green << 8) | blue;
}
user1009569
  • 477
  • 6
  • 22

1 Answers1

2

You can use java.awt.Color.RGBtoHSB(...) You can find the relevant documentation for it here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html

Then it just becomes trivial of generating a random color.

int red = (int) (Math.random() * 256)
int green = (int) (Math.random() * 256)
int blue = (int) (Math.random() * 256)

Then convert directly. Note that there is also a HSBtoRGB(...) function in the same class.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • No, I'm looking to generate a random HSV value. I don't want to get "random" RGB values and then convert to HSV then back again. – user1009569 Jul 10 '13 at 17:51
  • Presumably you could easily generate a random RGB value, as shown in my snippet above, then convert these RGB values to your HSB value. I have no idea what HSV is, and Google confirms that you probably mean HSB. See here: http://stackoverflow.com/questions/2399150/convert-rgb-value-to-hsv – Kon Jul 10 '13 at 17:55
  • Yeah, HSV and HSB are the same, and I'm needing to get random values for HSB. Your answer is correct but I need random HSB values, thanks. – user1009569 Jul 10 '13 at 17:58
  • If you need to generate random HSB values directly, then what you need to know are the upper and lower bounds for hue, saturation and brightness. Since they're float values I assume that all 3 values go from 0 to 1? If that's the case, then you generate each individually with h = Math.random(), s = Math.random(), b = Math.random() as this generates a random double between 0 and 1, which you can explicitly cast to a float or whatever you need. – Kon Jul 10 '13 at 18:03
  • Thanks a lot, the bounds for hue is 6, which is why I've got int hue = (int) (Math.random() * 6.0f); but it's not working for me unfortunately :(. – user1009569 Jul 10 '13 at 18:06
  • I see your problem. You're converting to HSB values that you're generating with Math.random() to integers explicitly. You need to store these values in a float or double. Remember that Math.random() returns a float value between 0 and 1. So assume it returns something like 0.335351, well you're immediately casting it to an (int), so that becomes 0 every time. Store it in a float and don't cast it to anything. – Kon Jul 10 '13 at 18:07
  • I feel silly haha, thanks for pointing that out – user1009569 Jul 10 '13 at 18:11
  • No problem, hope it's working now – Kon Jul 10 '13 at 18:11