gpt4 book ai didi

java - 十六进制字符串到颜色名称

转载 作者:行者123 更新时间:2023-12-01 06:22:01 24 4
gpt4 key购买 nike

这不是一个重复的问题,我知道有人问了一些像我一样的问题,但我需要有关如何实现到我的应用程序的帮助。

我正在尝试制作一个可以捕获颜色的Android应用程序,但我只发现如何获取十六进制值,但我真的需要颜色的名称!请帮忙,某人!下面是整个代码,但它说的是

public static String makeHexString(int value) {
return "#" + Integer.toHexString(value).substring(2);}

这里的这一行,Integer.toHexString 如何才能获取颜色名称而不是十六进制值?

 public class ColorItem implements Parcelable {

protected final long mId;

protected int mColor;

protected String mName;

protected final long mCreationTime;

protected transient String mHexString;

protected transient String mRgbString;

protected transient String mHsvString;

public ColorItem(long id, int color) {
mId = id;
mColor = color;
mCreationTime = System.currentTimeMillis();
}

private ColorItem(Parcel in) {
this.mId = in.readLong();
this.mColor = in.readInt();
this.mCreationTime = in.readLong();
this.mName = in.readString();
}

public ColorItem(int color) {
mId = mCreationTime = System.currentTimeMillis();
mColor = color;
}

public long getId() {
return mId;
}

public int getColor() {
return mColor;
}

public void setColor(int color) {
if (mColor != color) {
mColor = color;
mHexString = makeHexString(mColor);
mRgbString = makeRgbString(mColor);
mHsvString = makeHsvString(mColor);
}
}

public long getCreationTime() {
return mCreationTime;
}

public String getHexString() {
if (mHexString == null) {
mHexString = makeHexString(mColor);
}
return mHexString;
}

public String getRgbString() {
if (mRgbString == null) {
mRgbString = makeRgbString(mColor);
}
return mRgbString;
}

public String getHsvString() {
if (mHsvString == null) {
mHsvString = makeHsvString(mColor);
}
return mHsvString;
}

public String getName() {
return mName;
}

public void setName(String name) {
mName = name;
}

public static String makeHexString(int value) {
return "#" + Integer.toHexString(value).substring(2);
}

public static String makeRgbString(int value) {
return "rgb(" + Color.red(value) + ", " + Color.green(value) + ", " + Color.blue(value) + ")";
}

public static String makeHsvString(int value) {
float[] hsv = new float[3];
Color.colorToHSV(value, hsv);
return "hsv(" + (int) hsv[0] + "°, " + (int) (hsv[1] * 100) + "%, " + (int) (hsv[2] * 100) + "%)";
}


@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.mId);
dest.writeInt(this.mColor);
dest.writeLong(this.mCreationTime);
dest.writeString(this.mName);
}

public static final Creator<ColorItem> CREATOR = new Creator<ColorItem>() {
public ColorItem createFromParcel(Parcel source) {
return new ColorItem(source);
}

public ColorItem[] newArray(int size) {
return new ColorItem[size];
}
};
}

最佳答案

以下是一些颜色名称:

public String getColorName(String hexColor) {
String name = "";
switch (hexColor.toLowerCase()) {
case "#ff0000":
name = "Red";
break;
case "#00ff00":
name = "Green";
break;
case "#0000ff":
name = "Blue";
break;
case "#ffffff":
name = "White";
break;
case "#000000":
name = "Black";
break;
default:
name = "Unknown";
break;
}
return name;
}

关于java - 十六进制字符串到颜色名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51314306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com