转换主要颜色为颜色名称

huangapple go评论83阅读模式
英文:

Convent dominant color to color name

问题

如何将主要颜色转换为颜色名称?

这个方法是从图像中找到主要颜色。

private void extractProminentColors(Bitmap bitmap){
    int defaultColor = 0x000000;
    Palette p = Palette.from(bitmap).generate();
    int VibrantColor = p.getVibrantColor(defaultColor);
    v1 = String.format("#%X", VibrantColor);
    colorv1 = v1;
    checkBox1.setBackgroundColor(VibrantColor);
    int MutedColorLight = p.getLightMutedColor(defaultColor);

    v2 = String.format("#%X", MutedColorLight);
    colorv2 = v2;
    checkBox2.setBackgroundColor(MutedColorLight);
    int MutedColorDark = p.getDarkMutedColor(defaultColor);

    v3 = String.format("#%X", MutedColorDark);
    colorv3 = v3;
    checkBox3.setBackgroundColor(MutedColorDark);
}

这是颜色名称的颜色列表数组:

final ArrayList<ColorName> colorList = new ArrayList<ColorName>();
colorList.add(new ColorName("Black", 0x00, 0x00, 0x00)); 
colorList.add(new ColorName("White", 0xff, 0xff, 0xff)); 
colorList.add(new ColorName("Gray", 0x80, 0x80, 0x80)); 
colorList.add(new ColorName("Navy" , 0x00, 0x00, 0x80)); 
colorList.add(new ColorName("Red", 0xff, 0x00, 0x00)); 
colorList.add(new ColorName("Orange", 0xff, 0x80, 0x00)); 
colorList.add(new ColorName("Yellow", 0xff, 0xff, 0x00)); 
colorList.add(new ColorName("Green", 0x00, 0xff, 0x00)); 
colorList.add(new ColorName("Blue", 0x00, 0x00, 0xff));
英文:

How can I convert a dominant color into a color name?

This Method is find dominant color from image.

private void extractProminentColors(Bitmap bitmap){
        int defaultColor = 0x000000;
        Palette p = Palette.from(bitmap).generate();
        int VibrantColor = p.getVibrantColor(defaultColor);
        v1 = String.format(&quot;#%X&quot;, VibrantColor);
        colorv1 = v1;
        checkBox1.setBackgroundColor(VibrantColor);
        int MutedColorLight = p.getLightMutedColor(defaultColor);

        v2 = String.format(&quot;#%X&quot;, MutedColorLight);
        colorv2 = v2;
        checkBox2.setBackgroundColor(MutedColorLight);
        int MutedColorDark = p.getDarkMutedColor(defaultColor);

        v3 = String.format(&quot;#%X&quot;, MutedColorDark);
        colorv3 = v3;
        checkBox3.setBackgroundColor(MutedColorDark);
    }

and this is ArrayList of color name

final ArrayList&lt;ColorName&gt; colorList = new ArrayList&lt;ColorName&gt;();
    colorList.add(new ColorName(&quot;Black&quot;, 0x00, 0x00, 0x00)); 
    colorList.add(new ColorName(&quot;White&quot;, 0xff, 0xff, 0xff)); 
    colorList.add(new ColorName(&quot;Gray&quot;, 0x80, 0x80, 0x80)); 
    colorList.add(new ColorName(&quot;Navy&quot; , 0x00, 0x00, 0x80)); 
    colorList.add(new ColorName(&quot;Red&quot;, 0xff, 0x00, 0x00)); 
    colorList.add(new ColorName(&quot;Orange&quot;, 0xff, 0x80, 0x00)); 
    colorList.add(new ColorName(&quot;Yellow&quot;, 0xff, 0xff, 0x00)); 
    colorList.add(new ColorName(&quot;Green&quot;, 0x00, 0xff, 0x00)); 
    colorList.add(new ColorName(&quot;Blue&quot;, 0x00, 0x00, 0xff)); 

答案1

得分: 1

作为图像中占主导地位的颜色很可能与列出的命名颜色并不完全匹配,因此您只能找到最接近的命名颜色。

编写一个名为 distance() 的方法来计算两种颜色的差异,并在 colorNames 列表中搜索距离主导颜色最小的条目。

英文:

As the dominant colors in the image most probably don't exactly match the listed named colors, you can only find the closest named color.

Write a distance() method that calculates how much two colors differ, and in the colorNames list, search for the entry where the distance to the dominant color is minimal.

答案2

得分: 0

如果您正在寻找一个能够完成这个任务的命令行工具:Cict

示例:

$ ./cict 000081
1	#000080	navyblue

正如您所见,您只需将一个24位的十六进制值传递给 cict,它会报告找到的颜色与目标颜色之间的距离(在此例中为1),实际颜色值(#000080)以及颜色名称(navyblue)。

(我是 Cict 的作者)

英文:

If you are looking for a command-line tool that does exactly that: Cict

Example:

$ ./cict 000081
1	#000080	navyblue

As you can see, you simple pass a 24-bit hex-value to cict and it reports the distance to the color found (1 in this case), the value of the actual color (#000080) and the name (navyblue).

(I am the author of Cict)

huangapple
  • 本文由 发表于 2020年9月30日 23:13:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64140613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定