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

2 篇文章 0 订阅
这篇博客介绍了如何在Android环境中使用Java实现RGB、HSL和HSV颜色空间之间的转换。提供了详细的代码实现,包括颜色转换方法以及限制值在指定范围内的函数,有助于理解颜色处理和在应用中进行颜色交互。
摘要由CSDN通过智能技术生成

本篇算法基于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
    评论
当我们谈论颜色时,常常会涉及到RGBHSVHSL这三种颜色空间。它们是用来描述和表示颜色的不同模型,每个模型都有其独特的特点和用途。 1. RGB(红绿蓝)颜色空间: RGB是一种加法混色模型,它将红、绿和蓝三原色的不同强度组合以创建各种颜色。在RGB颜色空间中,每个颜色由一个红色(R)、绿色(G)和蓝色(B)的强度值组成。这些强度值通常在0到255之间,表示了每种原色的相对强度。通过调整这三种原色的强度,我们可以混合出各种不同的颜色。 2. HSV(色调、饱和度、亮度)颜色空间: HSV是一种将颜色描述为色调(H)、饱和度(S)和亮度(V)的模型。色调表示颜色在色轮上的位置,取值范围为0到360度。饱和度表示颜色的纯度或者灰度的量度,取值范围为0到1。亮度表示颜色的明亮程度,取值范围为0到1。HSV颜色空间常用于调整颜色的色调、饱和度和亮度,使得对颜色的控制更加直观。 3. HSL(色调、饱和度、亮度)颜色空间: HSL是一种将颜色描述为色调(H)、饱和度(S)和亮度(L)的模型。色调表示颜色在色轮上的位置,取值范围为0到360度。饱和度表示颜色的纯度或者灰度的量度,取值范围为0到1。亮度表示颜色的明亮程度,取值范围为0到1。与HSV相比,HSL颜色空间更加注重描述颜色的亮度。 这三种颜色空间各有其优势和适用场景。RGB常用于计算机图形学和显示器等领域,HSVHSL则常用于图像处理、调色板设计和艺术创作等领域。通过在不同的颜色空间之间转换,我们可以更好地控制和表达各种不同的颜色
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Pierce°ღ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值