1

This is not a duplicated question, I know that were some questions asked like mine, but I need help on how I can implement to my app.

I'm trying to make a android app that can capture the colors, but i've only discoverd how I can get the Hex value, but I really need the name of the color! please help, someone! Below it's the whole 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 make it to get the color name instead hex 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];
        }
    };
}
Pedro Grilo
  • 19
  • 1
  • 4
  • Do you want name of the color from its hex code ? – Raj Jul 12 '18 at 21:01
  • yes please! I want to be able to show on my app the name of the color instead of an hex value or RGB – Pedro Grilo Jul 12 '18 at 21:02
  • Can you clarify with an example? – Bhavita Lalwani Jul 12 '18 at 21:17
  • so for example, my app picks the color that's in the center of the screen, but I was able to capture in RGB, HEX and HSV, but what I really want is that it shows automatically the name of the color, not in those values mentioned before. I've thought of doing, like a range of color are that certain name, but I just don't know how to apply it, help ! – Pedro Grilo Jul 12 '18 at 21:23
  • 1
    Are you trying to generate names for each of the more than 16 million colors that can come from a `#XXXXXX` hex string? For example, what name do you expect for `#FFFFFE`? – Krease Jul 12 '18 at 21:27
  • @Krease no I mean, this monday I'm going to present my android app, to a jury and my app needs the names of colors, how can I do that? I capture the color or the value from my camera, and it needs to be the color name – Pedro Grilo Jul 12 '18 at 21:49
  • and it's there another way where can like search the web and get the name? – Pedro Grilo Jul 12 '18 at 21:50
  • WHat you want isn't possible. What color is some random hex code? Not all colors have names, and many will have different names in different cultures. I don't just mean languages- I mean some cultures don't have the concept of yellow. They see it as a shade of green. See https://www.sapiens.org/language/color-perception/ If you want to add this concept to your app, you're going to have to define what ranges of hex codes belong to what names. – Gabe Sechan Jul 12 '18 at 21:53
  • @GabeSechan Yeah I totally understand, there's not names for every color, but I know that there's a way to identify the closest to a known color, but I don't how to! – Pedro Grilo Jul 12 '18 at 21:56
  • @PedroGrilo There isn't, because there isn't even a universal definition of what a color is. Its cultural, not scientific. You would have to write any such mapping yourself. But a decent start at an algorithm would be least difference from a set of defined colors. – Gabe Sechan Jul 12 '18 at 21:57
  • @GabeSechan I need something like [this](https://stackoverflow.com/a/20670056/9888243) – Pedro Grilo Jul 12 '18 at 21:59

3 Answers3

0

Here are a few color names:

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;
}
0

What makes this really hard is that generally we describe colours as a specific hex value. There are 2^24 different hex values but only like ~200 named colours.

What you could do is think about this geometrically using the concept of a colour cube. It has 3 dimensions, one for red, one for blue and one for green. If you also had a list of the named colours and how much R, G and B they each contain you can view those as points within the cube. Then for any colour you could find the nearest point and that would be a good estimation of the colour. This would result in a loss of information however because before you have so many possible values (2^24) but after you only have ~200.

In order to do this you first need to parse the hex code into a a 3-d point and also parse each of your colours from your colour list into a 3-d point. For example #ff0024 would be equal to [255, 0, 36].

Now you need to loop through your list of named colours and find the colour name with the minimum distance from your point. I'll give some pseudocode to explain this.

int [][] colorPoints = {{255, 255, 255}, {0, 0, 0}, ...};
String [] colorNames = {"white", "black", ...}; // These two arrays have the same length of N

int [] myColor = parseHexTo3dPoint(hexColorString); // Returns int array of length 3

double minDistance = Double.MAX_VALUE;
String closestColorName = colorNames[0];
for (int i = 0; i < colorNames.length; i++){
    int [] color = colorPoints[i];
    double d0 = (color[0] - myColor[0]);
    double d1 = (color[1] - myColor[1]);
    double d2 = (color[2] - myColor[2]);
    double distance = Math.sqrt(d0*d0 + d1*d1 + d2*d2);
    if (distance < minDistance){
        minDistance = distance;
        closestColorName = colorNames[i];
    }
}

return closestColorName;
Aidan Rosswood
  • 1,212
  • 1
  • 10
  • 21
-2

Try Color class method:

public static int parseColor (String colorString)

From Android documentation:

Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'

Eg:-

int myColor = Color.parseColor("#3F51B5");
myView.setBackgroundColor(myColor);

According to your code:

public static int makeHexString(int value) {
int myColor = Color.parseColor("#" + 
Integer.toHexString(value).substring(2));
return myColor;
}
Raj
  • 2,997
  • 2
  • 12
  • 30
  • but I want it to take a value from my camera and show a color like this public static String makeHexString(int value) { return "#" + Integer.toHexString(value).substring(2);} it returns the Integer.toHexString(value) and I wanted a name – Pedro Grilo Jul 12 '18 at 21:10
  • OP is trying to turn hex value into a string like “red”, not like you’re doing here – Krease Jul 12 '18 at 21:23
  • I have updated the answer. Do check @Pedro Grilo – Raj Jul 12 '18 at 21:26
  • You’re still converting to an int, not a String. See the answers on the duplicate: https://stackoverflow.com/questions/4126029/convert-rgb-values-to-color-name – Krease Jul 12 '18 at 21:32
  • @Krease how can implement cindyxiaoxiaoli code to mine? – Pedro Grilo Jul 12 '18 at 21:52