本篇算法基于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;
}
}