android - 如何使用 RGB 匹配两个几乎相同的匹配颜色

标签 android eclipse

我正在开发一个应用程序,其中有一个元素周期表,其中元素周期表中的每个元素都有不同的颜色。现在我向用户展示一个颜色选择器,用户从选择器中选择一种颜色,我将在元素周期表中匹配所选颜色并显示与其对应的元素。我知道如何从选择器中选择颜色并匹配两种颜色。现在我的问题是假设元素周期表中的一个元素是红色的#FF0000,如果用户选择红色而不是完全相同的红色代码(#FF0000)。现在在这种情况下,用户不太可能从选择器中选择准确的颜色。他可能会选择代码略有不同的类似红色。那么在这种情况下我将如何匹配。有没有一种方法可以在所有可能的 RGB 范围内匹配红色或任何特定的颜色,这样如果用户选择该范围内的任何颜色,我可以将它匹配到表格中的任何特定颜色。

最佳答案

第 1 步:提取用户选择的颜色的 3 个 RGB 值。

int colorSelected = Color.RED;  //#FF0000
int r = (colorSelected >> 16) & 0xFF;  //255
int g = (colorSelected >> 8) & 0xFF;   //0
int b = (colorSelected >> 0) & 0xFF;   //0

第 2 步:定义阈值

int threshold = 15

第三步:对于元素周期表中的每一种颜色,重复第一步,匹配每一个值都在范围内

int currentCellColor = getCellColor();  //Let's say #FF0100
int currentRed = (currentCellColor >> 16) & 0xFF;  //255
int currentGreen = (currentCellColor >> 8) & 0xFF;   //0
int currentBlue = (currentCellColor >> 0) & 0xFF;   //0
if (((currentRed >= (r - threshold)) && (currentRed <= (r + threshold))
  && ((currentGreen >= (g - threshold)) && (currentGreen <= (g + threshold))
  && ((currentBlue >= (b - threshold)) && (currentBlue <= (b + threshold))) {
  //CurrentColor is within the desired threshold
} else {
  //CurrentColor is not within the desired threshold
}

从技术上讲,色谱不应是线性的,但您应该通过这种方式获得良好的近似值。如需进一步研究,您可以浏览https://en.wikipedia.org/wiki/Color_difference

关于android - 如何使用 RGB 匹配两个几乎相同的匹配颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792127/

相关文章:

android - 每次调用时如何在 Observable.create 中运行该函数

c++ - 一个简短的 c++ 文件和 makefile : I can make in the shell,,但在 Eclipse 中构建它时出现很多错误

java - GAE 开始使用 Java,Eclipse Indigo - 无法编译为 1.6

Android - AutoCompleteTextView 仅在退格时有效

java - 设置 "On start"fragment

java - 发现如果 Minus 出现在被认为是负数的数字的开头

android - Youtube API 视频在非全屏时停止

java - IBM process server v8.5.5 - 由 : java. lang.IllegalStateException 引起:不支持 BO 服务

java - 调试 Maven 项目终止 JFrame 创建

eclipse - 请求的资源不可用。 Apache Tomcat/7.0.42