Get color name from HEX value

Asked

Viewed 374 times

0

I’m trying to make an Android app that can capture the colors but I just figured out how I can get the HEX value but I really need the color name! Please I need an alternative that shows the name of the colors

Below is all the code, but where it says

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

this line here, Integer.toHexString, how can I do this to get color name instead of hexadecimal value?

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];
        }
    };
}
  • 1

    I think not all possible color values have names. In fact, I think only a very limited set has. I found this list but I don’t know if it’s something "official" (in the sense of having a norm that defines the "correct" names of each color) - so I find it easier to make a mapping for those that have a name, and for the others, use the same hexa value. Even so it will be something arbitrary, eg: in the link I passed, ADD8E6 is "light blue", but if you change to ADD8E7, many (myself included) can not notice the difference. Still light blue? I don’t know

  • so what if I make a sort of range between values? please I need an alternative that shows the name of colors!!

  • 1

    I think the easiest thing is to create an actual mapping (be it with a java.util.Map, or via some configuration file, etc.), which defines the name for each value, will be something created "at hand", because I don’t know anything that does this automatically. And I don’t think there’s an "official" name for all colors, so maybe the range of values is an alternative.

  • for example this list of names? can help me implement this in my program?

  • First I would better define the scope. You need the name of whichever possible color value? (using RGB, are more than 16 million possible values, it is impracticable that each has its own name). Either you define color ranges that have the same name (and it’s still a job), or you define values that have names and values that don’t. Then I would use a map, for example to map each value with its name.

  • how can I implement that list I sent earlier?

  • See the tutorial (link from my previous comment) on how to use Map. Then use hexa code as key and name as value. I would start there

  • @hkotsubo I made a list of XML names with their Hexadecimla codes, it will be possible to work with it?

  • @hkotsubo I’m sorry but I didn’t get any of this from " The Map Interface"

  • http://www.guj.com.br/t/hashmap/107481

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.