简体   繁体   中英

Java: Cycling through a particular color with HSV

So I've been trying to cycle through colors within a certain degree range using HSV, but I can't seem to transition smoothly between the colors. Ideally I would like to be able to move within a specific degree range (80-140) and cycle the saturation and value between .5 - 1. I honestly have no idea where to go from here and I'm eager to learn how.

int[] colors = HsvToRgb(h,s ,v );

The methods I used:

public static int[] HsvToRgb(double h, double S, double V) {
    double H = h;
    while (H < 0) {
        H += 360;
    }
    while (H >= 360) {
        H -= 360;
    }
    double R = 0, G = 0, B = 0;
    if (V <= 0) {
        R = G = B = 0;
    } else if (S <= 0) {
        R = G = B = V;
    } else {
        double hf = H / 60.0;
        int i = (int) Math.floor(hf);
        double f = hf - i;
        double pv = V * (1 - S);
        double qv = V * (1 - S * f);
        double tv = V * (1 - S * (1 - f));
        switch (i) {
            case 0:
                R = V;
                G = tv;
                B = pv;
                break;
            case 1:
                R = qv;
                G = V;
                B = pv;
                break;
            case 2:
                R = pv;
                G = V;
                B = tv;
                break;
            case 3:
                R = pv;
                G = qv;
                B = V;
                break;
            case 4:
                R = tv;
                G = pv;
                B = V;
                break;
            case 5:
                R = V;
                G = pv;
                B = qv;
                break;
            case 6:
                R = V;
                G = tv;
                B = pv;
                break;
            case -1:
                R = V;
                G = pv;
                B = qv;
                break;
        }
    }
    int r = Clamp((int) (R * 255.0));
    int g = Clamp((int) (G * 255.0));
    int b = Clamp((int) (B * 255.0));
    return new int[]{r, g, b};
}

public static int Clamp(int i) {
    if (i < 0) {
        return 0;
    }
    if (i > 255) {
        return 255;
    }
    return i;
}

Check out HSL Color . Its API allows you to easily change the color using methods like:

  1. adjustHue – specify the degree of a color
  2. adjustLuminance – specify an absolute percentage of luminance
  3. adjustSaturation – specify an absolute percentage of saturation
  4. adjustShade – adjusting the shade will give you a darker color. You control how much darker the color should be by specifying the adjustmnt percent.
  5. adjustTone – adjusting the tone will give you a lighter color. You control how much lighter the color should be by specifying the adjustment percent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM