Roger
Roger

Reputation: 6527

How to adjust the hue of a color code?

Maybe someone know of a way in Java (Android) to apply HUE to a color code?

For example if I have #1589FF and apply 180 HUE, I should get #FF8B14.

Upvotes: 7

Views: 8233

Answers (2)

ucMax
ucMax

Reputation: 5428

Another peachy way to do that

    /**
     * @param color the ARGB color to convert. The alpha component is ignored
     * @param hueFactor The factor of hue, an int [0 .. 360)
     * @return new color with the specified hue.
     */
    public int hue(int color, int hueFactor) {
        float[] hsl= new float[3];
        ColorUtils.colorToHSL(color,hsl);
        hsl[0]=hueFactor;
        return ColorUtils.HSLToColor(hsl);
    }

Upvotes: 0

aioobe
aioobe

Reputation: 420951

This should do the trick:

Color c = new Color(0x15, 0x89, 0xFF);

// Get saturation and brightness.
float[] hsbVals = new float[3];
Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsbVals);

// Pass .5 (= 180 degrees) as HUE
c = new Color(Color.HSBtoRGB(0.5f, hsbVals[1], hsbVals[2]));

Upvotes: 9

Related Questions