admin管理员组

文章数量:1125480

Helo all. I am working on a project, and i want to convert the argb value into descriptive text color and show it to the user.

I am using palette api to extract argb color from an image and I want to convert it into descriptive text color but I have no idea how to do that. Can anyone help me with that. I tried by giving pre defined values but didn't got desired result. I would like to know if there is any api or library which can do so or any alternative to do it.

Palette palette = Palette.from(bitmap).generate(); 

binding.colour.setText(argbToTextColor(palette.getDominantColor(0)));


public String argbToTextColor(int argb) {
        int rgb = argb & 0x00FFFFFF;
        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = rgb & 0xFF;

        if (red < 50 && green < 50 && blue < 50) {
            return "Black";
        } else if (red > 200 && green > 200 && blue > 200) {
            return "White";
        } else if (red > green && red > blue) {
            return "Red";
        } else if (green > red && green > blue) {
            return "Green";
        } else if (blue > red && blue > green) {
            return "Blue";
        } else if (red > 200 && green > 200) {
            return "Yellow";
        } else if (red > 200 && blue > 200) {
            return "Magenta";
        } else if (green > 200 && blue > 200) {
            return "Cyan";
        } else if (red > 150 && green > 100 && blue < 50) {
            return "Orange";  
        } else if (red < 100 && green < 100 && blue < 100) {
            return "Gray";  
        } else if (red > 150 && green > 50 && blue > 150) {
            return "Purple";
        } else if (red < 100 && green < 100 && blue > 150) {
            return "Pink";  
        } else if (red > 100 && green > 50 && blue < 50) {
            return "Brown"; 
        } else {
            return "Unknown Color";
        }
    }

Helo all. I am working on a project, and i want to convert the argb value into descriptive text color and show it to the user.

I am using palette api to extract argb color from an image and I want to convert it into descriptive text color but I have no idea how to do that. Can anyone help me with that. I tried by giving pre defined values but didn't got desired result. I would like to know if there is any api or library which can do so or any alternative to do it.

Palette palette = Palette.from(bitmap).generate(); 

binding.colour.setText(argbToTextColor(palette.getDominantColor(0)));


public String argbToTextColor(int argb) {
        int rgb = argb & 0x00FFFFFF;
        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = rgb & 0xFF;

        if (red < 50 && green < 50 && blue < 50) {
            return "Black";
        } else if (red > 200 && green > 200 && blue > 200) {
            return "White";
        } else if (red > green && red > blue) {
            return "Red";
        } else if (green > red && green > blue) {
            return "Green";
        } else if (blue > red && blue > green) {
            return "Blue";
        } else if (red > 200 && green > 200) {
            return "Yellow";
        } else if (red > 200 && blue > 200) {
            return "Magenta";
        } else if (green > 200 && blue > 200) {
            return "Cyan";
        } else if (red > 150 && green > 100 && blue < 50) {
            return "Orange";  
        } else if (red < 100 && green < 100 && blue < 100) {
            return "Gray";  
        } else if (red > 150 && green > 50 && blue > 150) {
            return "Purple";
        } else if (red < 100 && green < 100 && blue > 150) {
            return "Pink";  
        } else if (red > 100 && green > 50 && blue < 50) {
            return "Brown"; 
        } else {
            return "Unknown Color";
        }
    }

Share Improve this question asked Jan 9 at 7:07 FarazFirozFarazFiroz 333 bronze badges New contributor FarazFiroz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 3

In C# there is a table of known colors, but there are 140 of them, also there is a library of Color Names with 30261 of them, but to cover all the variants with transparency and 256^3 values (16 777 216) is unlikely to work.

Since there are way more color values than names (as already said in another answer), the best you can do is probably the following:

  1. Make a table of known colors (e.g. based on this)
  2. Choose the color distance metric that works best for your use case
  3. Find the known color with the minimal distance from the given color value[*]

[*] For efficient search you might want to utilize a vector database.

本文标签: javaHow to conver argb into descriptive text colorStack Overflow