RGB、HSL、HSV互相转化【Java】

2 篇文章 0 订阅

本篇算法基于Android内置方法编写而成,且与APP交互认证

见以下代码

import java.awt.*;
import java.math.BigDecimal;

/**
 * RGB、HSL、HSV互相转化
 *
 * @author Pierce.Cai
 * @createTime 2022/11/16
 */
public class test
{
    public static void main(String[] args)
    {
    }

    public static float constrainFloat(float amount, float low, float high)
    {
        return amount < low ? low : Math.min(amount, high);
    }

    public static int constrainInt(int amount, int low, int high)
    {
        return amount < low ? low : Math.min(amount, high);
    }

    public static Hsl rgb2Hsl(Rgb rgb)
    {

        if (rgb == null)
        {
            return null;
        }

        final float r = rgb.getR() / 255f;
        final float g = rgb.getG() / 255f;
        final float b = rgb.getB() / 255f;

        final float max         = Math.max(r, Math.max(g, b));
        final float min         = Math.min(r, Math.min(g, b));
        final float deltaMaxMin = max - min;

        float h;
        float s;
        float l = (max + min) / 2f;

        if (BigDecimal.valueOf(max)
                      .equals(BigDecimal.valueOf(min)))
        {
            // Monochromatic
            h = s = 0f;
        }
        else
        {
            if (BigDecimal.valueOf(max)
                          .equals(BigDecimal.valueOf(r)))
            {
                h = ((g - b) / deltaMaxMin) % 6f;
            }
            else if (BigDecimal.valueOf(max)
                               .equals(BigDecimal.valueOf(g)))
            {
                h = ((b - r) / deltaMaxMin) + 2f;
            }
            else
            {
                h = ((r - g) / deltaMaxMin) + 4f;
            }

            s = deltaMaxMin / (1f - Math.abs(2f * l - 1f));
        }

        h = (h * 60f) % 360f;
        if (h < 0)
        {
            h += 360d;
        }
        h = h / 360f;
        return new Hsl(h, s, l);
    }

    public static Rgb hsl2Rgb(Hsl hsl)
    {
        if (hsl == null)
        {
            return null;
        }

        final float h = hsl.getH() * 360f;
        final float s = hsl.getS();
        final float l = hsl.getL();

        final float c = (1f - Math.abs(2 * l - 1f)) * s;
        final float m = l - 0.5f * c;
        final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

        final int hueSegment = (int) h / 60;

        int r = 0;
        int g = 0;
        int b = 0;

        switch (hueSegment)
        {
            case 0:
                r = Math.round(255 * (c + m));
                g = Math.round(255 * (x + m));
                b = Math.round(255 * m);
                break;
            case 1:
                r = Math.round(255 * (x + m));
                g = Math.round(255 * (c + m));
                b = Math.round(255 * m);
                break;
            case 2:
                r = Math.round(255 * m);
                g = Math.round(255 * (c + m));
                b = Math.round(255 * (x + m));
                break;
            case 3:
                r = Math.round(255 * m);
                g = Math.round(255 * (x + m));
                b = Math.round(255 * (c + m));
                break;
            case 4:
                r = Math.round(255 * (x + m));
                g = Math.round(255 * m);
                b = Math.round(255 * (c + m));
                break;
            case 5:
            case 6:
                r = Math.round(255 * (c + m));
                g = Math.round(255 * m);
                b = Math.round(255 * (x + m));
                break;

            default:
                break;
        }

        return new Rgb(r, g, b);
    }

    public static Rgb hsv2Rgb(Hsv hsvColor)
    {
        if (hsvColor == null)
        {
            return null;
        }
        Color color = Color.getHSBColor(hsvColor.getH(), hsvColor.getS(), hsvColor.getV());
        return new Rgb(color.getRed(), color.getGreen(), color.getBlue());
    }

    public static Hsv rgb2Hsv(Rgb rgbColor)
    {
        if (rgbColor == null)
        {
            return null;
        }
        float[] hsbvals = {0, 0, 0};
        Color.RGBtoHSB(rgbColor.getR(), rgbColor.getG(), rgbColor.getB(), hsbvals);

        return new Hsv(hsbvals[0], hsbvals[1], hsbvals[2]);
    }
}

class Hsl
{
    private float h;
    private float s;
    private float l;

    public Hsl(float h, float s, float l)
    {
        this.h = test.constrainFloat(h, 0, 1);
        this.s = test.constrainFloat(s, 0, 1);
        this.l = test.constrainFloat(l, 0, 1);
    }

    public float getH()
    {
        return h;
    }

    public void setH(float h)
    {
        this.h = h;
    }

    public float getS()
    {
        return s;
    }

    public void setS(float s)
    {
        this.s = s;
    }

    public float getL()
    {
        return l;
    }

    public void setL(float l)
    {
        this.l = l;
    }
}

class Hsv
{
    private float h;
    private float s;
    private float v;

    public Hsv(float h, float s, float v)
    {
        this.h = test.constrainFloat(h, 0, 1);
        this.s = test.constrainFloat(s, 0, 1);
        this.v = test.constrainFloat(v, 0, 1);
    }

    public float getH()
    {
        return h;
    }

    public void setH(float h)
    {
        this.h = h;
    }

    public float getS()
    {
        return s;
    }

    public void setS(float s)
    {
        this.s = s;
    }

    public float getV()
    {
        return v;
    }

    public void setV(float v)
    {
        this.v = v;
    }
}

class Rgb
{
    private int r;
    private int g;
    private int b;

    public Rgb(int r, int g, int b)
    {
        this.r = test.constrainInt(r, 0, 255);
        this.g = test.constrainInt(g, 0, 255);
        this.b = test.constrainInt(b, 0, 255);
    }

    public int getR()
    {
        return r;
    }

    public void setR(int r)
    {
        this.r = r;
    }

    public int getG()
    {
        return g;
    }

    public void setG(int g)
    {
        this.g = g;
    }

    public int getB()
    {
        return b;
    }

    public void setB(int b)
    {
        this.b = b;
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pierce°ღ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值