PHP:将 html 十六进制颜色分类为几个简单字符串值的函数

发布于 2024-12-20 21:36:50 字数 391 浏览 4 评论 0原文

是否可以将 html 十六进制颜色分类为简单的字符串值?

例如,颜色#CC3333,它不是完全红色,但作为人类我们可以假设它是红色的。颜色#CCCCCC可以归类为白色,因为我不希望涉及黑色​​或灰色。

可能的简单值至少包括:

  • 红色
  • 白色
  • 绿色

分类越多越好,但我至少想要这些颜色。

能做到吗?

可选信息:

我正在创建一个通过网络摄像头捕获图片的网络应用程序。用户可以将白色或红色的纸放在网络摄像头上,应用程序会检测图像的主要颜色。然后,用户将根据其颜色被重定向到不同的选项。我已经完成了颜色检测,但我只想将其分类为几种颜色,红,白,绿。

Is it possible to classify html hex colours into simple string values??

For example, the colour #CC3333, it's not completely red, but as a human we can assume that it's red. The colour #CCCCCC can be classified as white, because I don't want black or grey involved.

Possible simple values at least consist of:

  • red
  • white
  • green

More classification is better, but I want at least these colours.

Can it be done?

Optional info:

I'm creating a web apps that capture the pictures through webcam. The user can hold a white or red paper to the webcam and the apps detect the image's major colour. Then the user will be redirected to a different options depending on their colour. I've done the colour detection, but I just want classify it into several colour only, red,white,and green.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放低过去 2024-12-27 21:36:50

这是一个有点主观的问题,因为是的,它可以做到,但具体如何做到这一点取决于您的具体应用 - 颜色本身就个人观察的方式而言是非常主观的。

您需要首先将字符串拆分为红色、绿色和蓝色分量:

$colourId = 'CC3333';
list($red, $green, $blue) = str_split($colourId, 2);

然后,将它们转换为整数可能是一个想法:

$red = hexdec($red);
$green = hexdec($green);
$blue = hexdec($blue);

然后您需要对其应用某种逻辑来确定您的哪个类它落入。如何做到这一点实际上取决于您,但也许您可以这样做:

if (max($red, $green, $blue) - min($red, $green, $blue) < 10) {
  // If the values are all within a range of 10, we'll call it white
  $class = 'white'; 
} else if (max($red, $green, $blue) == $red) {
  // If red is the strongest, call it red
  $class = 'red'; 
} else if (max($red, $green, $blue) == $green) {
  // If green is the strongest, call it green
  $class = 'green'; 
} else if (max($red, $green, $blue) == $blue) {
  // If blue is the strongest, call it blue
  $class = 'blue'; 
}

This is a somewhat subjective question, because yes it can be done, but exactly how you would do it would depend on your specific application - colour itself is very subjective in the way that it is observed by the individual.

You would need to start by splitting the string into it's red, green and blue components:

$colourId = 'CC3333';
list($red, $green, $blue) = str_split($colourId, 2);

Then, it would probably be an idea to convert them to integers:

$red = hexdec($red);
$green = hexdec($green);
$blue = hexdec($blue);

Then you need to apply some sort of logic to it to determine which one of your classes it falls into. How you do this is really up to you, but maybe you could do something like this:

if (max($red, $green, $blue) - min($red, $green, $blue) < 10) {
  // If the values are all within a range of 10, we'll call it white
  $class = 'white'; 
} else if (max($red, $green, $blue) == $red) {
  // If red is the strongest, call it red
  $class = 'red'; 
} else if (max($red, $green, $blue) == $green) {
  // If green is the strongest, call it green
  $class = 'green'; 
} else if (max($red, $green, $blue) == $blue) {
  // If blue is the strongest, call it blue
  $class = 'blue'; 
}
江挽川 2024-12-27 21:36:50

RGB模型中的颜色很难分类,最好将颜色转换为HSL或HSV模型,然后才能对颜色进行分类。有关更多信息,您可以查看网址:http://en.wikipedia.org/wiki/Color_model

it's hard to classified the color in RGB model it's better convert the color to HSL or HSV model , and then you can classified the color. for more information you can look at:http://en.wikipedia.org/wiki/Color_model

人间☆小暴躁 2024-12-27 21:36:50

您首先需要将十六进制格式转换为 RGB 值。简单的 Google 搜索就出现了此页面。我还没有测试过它,但如果它不能正常工作,那么我相信你可以找到一个可以正常工作的不同的。

获得 RGB 值后,您需要定义颜色范围。以下代码以 63.75 为间隔创建颜色范围(即每种颜色 4 个范围,因此 4*4*4 = 64 个总范围):

function findColorRange($colorArray){

    //assume $colorArray has the format [r,g,b], where r, g, and b are numbers in the range 0 - 255

    for($i = 0; $i < 256; $i += 51){ //find red range first
        if($colorArray[0] <= $i + 51/2 && $colorArray[0] >= $i - 51/2){
            for($n = 51; $n < 256; $n += 51){ //green
                if($colorArray[1] <= $n + 51/2 && $colorArray[1] >= $n - 51/2){
                    for($z = 51; $z < 256; $z += 51){ //blue
                        if($colorArray[2] <= $z + 51/2 && $colorArray[2] >= $z - 51/2){
                            return array($i,$n,$z);
                        }
                    }
                }
            }
        }
    }
}

上述函数将返回一个定义相关颜色的颜色范围的数组。从那里,您可以将可能的范围映射到您想要的任何字符串。这可能最容易通过创建一个关联数组来实现,其中键是 r、g、b 值,值是字符串。例如:

$colorMap = array(
    '0,0,0' => 'white',
    '51,0,0' => 'light gray'
)

You first need to convert the hex format to rgb values. A simple Google search turned up this page. I haven't tested it, but if it doesn't work correctly then I'm sure you can find a different one that does.

Once you have the rgb values, you need to define your color ranges. The following code creates color ranges at every interval of 63.75 (that's 4 ranges per color, so 4*4*4 = 64 total ranges):

function findColorRange($colorArray){

    //assume $colorArray has the format [r,g,b], where r, g, and b are numbers in the range 0 - 255

    for($i = 0; $i < 256; $i += 51){ //find red range first
        if($colorArray[0] <= $i + 51/2 && $colorArray[0] >= $i - 51/2){
            for($n = 51; $n < 256; $n += 51){ //green
                if($colorArray[1] <= $n + 51/2 && $colorArray[1] >= $n - 51/2){
                    for($z = 51; $z < 256; $z += 51){ //blue
                        if($colorArray[2] <= $z + 51/2 && $colorArray[2] >= $z - 51/2){
                            return array($i,$n,$z);
                        }
                    }
                }
            }
        }
    }
}

The above function would return an array that defines the color range of the color in question. From there, you could map the possible ranges to whatever strings you want. This would probably be most easily achieved by creating an associative array where the keys are the r,g,b values, and the values are the string. For example:

$colorMap = array(
    '0,0,0' => 'white',
    '51,0,0' => 'light gray'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文