php函数颜色问题

发布于 2024-11-25 07:14:50 字数 10129 浏览 1 评论 0原文

我创建了一个基于 RGB 颜色返回颜色的函数(使用 @ 参数)

但现在我对这种颜色(100,76,66)有问题。这是棕色,但函数返回深橄榄绿。

我的功能有错误吗?

例子:

$str_color = getColorNameByRgbColors('100,76,66'); echo $str_color;

function getColorNameByRgbColors($str_comma_sep_rbg) { /* php function colorname problem */

    $arr_input = explode(',', $str_comma_sep_rbg);

    // Dark Olive Green
    // based on -->     http://www.w3.org/TR/SVG/types.html#ColorKeywords
    $arr_colors = array(
            'Alice Blue'            =>  array(240, 248, 255),
            'Antique White'         =>  array(250, 235, 215),
            'aqua'                  =>  array(0, 255, 255), // ***
            'Aquamarine'            =>  array(127, 255, 212),
            'Azure'                 =>  array(240, 255, 255),
            'Beige'                 =>  array(245, 245, 220),
            'Bisque'                =>  array(255, 228, 196),
            'Black'                 =>  array(0, 0, 0),
            'Blanched Almond'       =>  array(255, 235, 205),
            'Blue'                  =>  array(0, 0, 255),
            'Blue Violet'           =>  array(138, 43, 226),
            'Brown'                 =>  array(165, 42, 42),
            'Burlywood'             =>  array(222, 184, 135),
            'Cadet Blue'            =>  array(95, 158, 160),
            'Chartreuse'            =>  array(127, 255, 0),
            'Chocolate'             =>  array(210, 105, 30),
            'Coral'                 =>  array(255, 127, 80),
            'Cornflower Blue'       =>  array(100, 149, 237),
            'Cornsilk'              =>  array(255, 248, 220),
            'crimson'               =>  array(220, 20, 60), // ***
            'Cyan'                  =>  array(0, 255, 255),
            'Dark blue'             =>  array(0, 0, 139),
            'Dark Cyan'             =>  array(0, 139, 139),
            'Dark Goldenrod'        =>  array(184, 134, 11),
            'Dark Green'            =>  array(0, 100, 0),
            'Dark Grey'             =>  array(169, 169, 169),
            'Dark Khaki'            =>  array(189, 183, 107),
            'Dark Magenta'          =>  array(139, 0, 139),
            'Dark Olive Green'      =>  array(85, 107, 47),
            'Dark Orange'           =>  array(255, 140, 0),
            'Dark Orchid'           =>  array(153, 50, 204),
            'Dark Red'              =>  array(139, 0, 0),
            'Dark Salmon'           =>  array(233, 150, 122),
            'Dark Sea Green'        =>  array(143, 188, 143),
            'Dark Slate Blue'       =>  array(72, 61, 139),
            'Dark Slate Gray'       =>  array(47, 79, 79),
            'Dark Turquoise'        =>  array(0, 206, 209),
            'Dark Violet'           =>  array(148, 0, 211),
            'Deep Pink'             =>  array(255, 20, 147),
            'Deep Sky Blue'         =>  array(0, 191, 255),
            'Dim Gray'              =>  array(105, 105, 105),
            'Dodger Blue'           =>  array(30, 144, 255),
            'Firebrick'             =>  array(178, 34, 34),
            'Floral White'          =>  array(255, 250, 240),
            'Forest Green'          =>  array(34, 139, 34),
            'fuchsia'               =>  array(255, 0, 255),     // **** 
            'Gainsboro'             =>  array(220, 220, 220),
            'Ghost White'           =>  array(248, 248, 255),
            'Gold'                  =>  array(255, 215, 0),
            'Goldenrod'             =>  array(218, 165, 32),
            'Grey'                  =>  array(128, 128, 128),
            'Green'                 =>  array(0, 128, 0),
            'Green Yellow'          =>  array(173, 255, 47),
            'Honeydew'              =>  array(240, 255, 240),
            'Hot Pink'              =>  array(255, 105, 180),
            'Indian Red'            =>  array(205, 92, 92),
            'Indigo'                =>  array(75, 0, 130),
            'Ivory'                 =>  array(255, 255, 240),
            'Khaki'                 =>  array(240, 230, 140),
            'Lavender'              =>  array(230, 230, 250),
            'Lavender Blush'        =>  array(255, 240, 245),
            'Lawn Green'            =>  array(124, 252, 0),
            'Lemon Chiffon'         =>  array(255, 250, 205),
            'Light Blue'            =>  array(173, 216, 230),
            'Light Coral'           =>  array(240, 128, 128),
            'Light Cyan'            =>  array(224, 255, 255),
            'Light Goldenrod Yellow'=>  array(250, 250, 210),
            'Light Gray'            =>  array(211, 211, 211),
            'Light Green'           =>  array(144, 238, 144),
            'Light Pink'            =>  array(255, 182, 193),
            'Light Salmon'          =>  array(255, 160, 122),
            'Light Sea Green'       =>  array(32, 178, 170),
            'Light Sky Blue'        =>  array(135, 206, 250),
            'Light Slate Gray'      =>  array(119, 136, 153),
            'Light Steel Blue'      =>  array(176, 196, 222),
            'Light Yellow'          =>  array(255, 255, 224),
            'Lime'                  =>  array(0, 255, 0),
            'Lime Green'            =>  array(50, 205, 50),
            'Linen'                 =>  array(250, 240, 230),
            'magenta'               =>  array(255, 0, 255),     //  ****d
            'Maroon'                =>  array(128, 0, 0),
            'Medium Aquamarine'     =>  array(102, 205, 170),
            'Medium Blue'           =>  array(0, 0, 205),
            'Medium Orchid'         =>  array(186, 85, 211),
            'Medium Purple'         =>  array(147, 112, 219),
            'Medium Sea Green'      =>  array(60, 179, 113),
            'Medium Slate Blue'     =>  array(123, 104, 238),
            'Medium Spring Green'   =>  array(0, 250, 154),
            'Medium Turquoise'      =>  array(72, 209, 204),
            'Medium Violet Red'     =>  array(199, 21, 133),
            'Midnight Blue'         =>  array(25, 25, 112),
            'Mint Cream'            =>  array(245, 255, 250),
            'Misty Rose'            =>  array(255, 228, 225),
            'Moccasin'              =>  array(255, 228, 181),
            'Navajo White'          =>  array(255, 222, 173),
            'Navy'                  =>  array(0, 0, 128),
            'Old Lace'              =>  array(253, 245, 230),
            'olive'                 =>  array(128, 128, 0), // ****
            'Olive Drab'            =>  array(107, 142, 35),
            'Orange'                =>  array(255, 165, 0),
            'Orange Red'            =>  array(255, 69, 0),
            'Orchid'                =>  array(218, 112, 214),
            'Pale Goldenrod'        =>  array(238, 232, 170),
            'Pale Green'            =>  array(152, 251, 152),
            'Pale Turquoise'        =>  array(175, 238, 238),
            'Pale Violet Red'       =>  array(219, 112, 147),
            'Papaya Whip'           =>  array(255, 239, 213),
            'Peach Puff'            =>  array(255, 218, 185),
            'Peru'                  =>  array(205, 133, 63),
            'Pink'                  =>  array(255, 192, 203),
            'Plum'                  =>  array(221, 160, 221),
            'Powder Blue'           =>  array(176, 224, 230),
            'Purple'                =>  array(128, 0, 128),
            'Red'                   =>  array(255, 0, 0),
            'Rosy Brown'            =>  array(188, 143, 143),
            'Royal Blue'            =>  array(65, 105, 225),
            'Saddle Brown'          =>  array(139, 69, 19),
            'Salmon'                =>  array(250, 128, 114),
            'Sandy Brown'           =>  array(244, 164, 96),
            'Sea Green'             =>  array(46, 139, 87),
            'Seashell'              =>  array(255, 245, 238),
            'Sienna'                =>  array(160, 82, 45),
            'Silver'                =>  array(192, 192, 192),
            'Sky Blue'              =>  array(135, 206, 235),
            'Slate Blue'            =>  array(106, 90, 205),
            'Slate Gray'            =>  array(112, 128, 144),
            'Snow'                  =>  array(255, 250, 250),
            'Spring Green'          =>  array(0, 255, 127),
            'Steel Blue'            =>  array(70, 130, 180),
            'Tan'                   =>  array(210, 180, 140),
            'teal'                  =>  array(0, 128, 128),
            'Thistle'               =>  array(216, 191, 216),
            'Tomato'                =>  array(255, 99, 71),
            'Turquoise'             =>  array(64, 224, 208),
            'Violet'                =>  array(238, 130, 238),
            'Wheat'                 =>  array(245, 222, 179),
            'White'                 =>  array(255, 255, 255),
            'White Smoke'           =>  array(245, 245, 245),
            'Yellow'                =>  array(255, 255, 0),
            'Yellow Green'          =>  array(154, 205, 50)
    );
    foreach($arr_colors as $key=>$color) {
        // $diff = abs($arr_input[0] - $color[0]) + abs($arr_input[1] - $color[1]) + abs($arr_input[2] - $color[2]);

        // Your code looks right, I ran it and got silver. However, I also tried squaring the differences in the components and got (what I thought was) an even better answer: lichtsteelblue. Here's the code I used:

        $diff = pow($arr_input[0] - $color[0],2) + pow($arr_input[1] - $color[1], 2) + pow($arr_input[2] - $color[2], 2);

        $array[$key] = $diff;
    }
    asort($array);
    reset($array);

    list($key, $diff) = each($array);
    return $key;
}

I have made a function that returns the color based on the RGB color (used @ the param)

But now i have a problem with this color (100,76,66). This is a brown color but the function returns Dark Olive Green.

Is there a mistake in my function ?

Example:

$str_color = getColorNameByRgbColors('100,76,66'); echo $str_color;

function getColorNameByRgbColors($str_comma_sep_rbg) { /* php function colorname problem */

    $arr_input = explode(',', $str_comma_sep_rbg);

    // Dark Olive Green
    // based on -->     http://www.w3.org/TR/SVG/types.html#ColorKeywords
    $arr_colors = array(
            'Alice Blue'            =>  array(240, 248, 255),
            'Antique White'         =>  array(250, 235, 215),
            'aqua'                  =>  array(0, 255, 255), // ***
            'Aquamarine'            =>  array(127, 255, 212),
            'Azure'                 =>  array(240, 255, 255),
            'Beige'                 =>  array(245, 245, 220),
            'Bisque'                =>  array(255, 228, 196),
            'Black'                 =>  array(0, 0, 0),
            'Blanched Almond'       =>  array(255, 235, 205),
            'Blue'                  =>  array(0, 0, 255),
            'Blue Violet'           =>  array(138, 43, 226),
            'Brown'                 =>  array(165, 42, 42),
            'Burlywood'             =>  array(222, 184, 135),
            'Cadet Blue'            =>  array(95, 158, 160),
            'Chartreuse'            =>  array(127, 255, 0),
            'Chocolate'             =>  array(210, 105, 30),
            'Coral'                 =>  array(255, 127, 80),
            'Cornflower Blue'       =>  array(100, 149, 237),
            'Cornsilk'              =>  array(255, 248, 220),
            'crimson'               =>  array(220, 20, 60), // ***
            'Cyan'                  =>  array(0, 255, 255),
            'Dark blue'             =>  array(0, 0, 139),
            'Dark Cyan'             =>  array(0, 139, 139),
            'Dark Goldenrod'        =>  array(184, 134, 11),
            'Dark Green'            =>  array(0, 100, 0),
            'Dark Grey'             =>  array(169, 169, 169),
            'Dark Khaki'            =>  array(189, 183, 107),
            'Dark Magenta'          =>  array(139, 0, 139),
            'Dark Olive Green'      =>  array(85, 107, 47),
            'Dark Orange'           =>  array(255, 140, 0),
            'Dark Orchid'           =>  array(153, 50, 204),
            'Dark Red'              =>  array(139, 0, 0),
            'Dark Salmon'           =>  array(233, 150, 122),
            'Dark Sea Green'        =>  array(143, 188, 143),
            'Dark Slate Blue'       =>  array(72, 61, 139),
            'Dark Slate Gray'       =>  array(47, 79, 79),
            'Dark Turquoise'        =>  array(0, 206, 209),
            'Dark Violet'           =>  array(148, 0, 211),
            'Deep Pink'             =>  array(255, 20, 147),
            'Deep Sky Blue'         =>  array(0, 191, 255),
            'Dim Gray'              =>  array(105, 105, 105),
            'Dodger Blue'           =>  array(30, 144, 255),
            'Firebrick'             =>  array(178, 34, 34),
            'Floral White'          =>  array(255, 250, 240),
            'Forest Green'          =>  array(34, 139, 34),
            'fuchsia'               =>  array(255, 0, 255),     // **** 
            'Gainsboro'             =>  array(220, 220, 220),
            'Ghost White'           =>  array(248, 248, 255),
            'Gold'                  =>  array(255, 215, 0),
            'Goldenrod'             =>  array(218, 165, 32),
            'Grey'                  =>  array(128, 128, 128),
            'Green'                 =>  array(0, 128, 0),
            'Green Yellow'          =>  array(173, 255, 47),
            'Honeydew'              =>  array(240, 255, 240),
            'Hot Pink'              =>  array(255, 105, 180),
            'Indian Red'            =>  array(205, 92, 92),
            'Indigo'                =>  array(75, 0, 130),
            'Ivory'                 =>  array(255, 255, 240),
            'Khaki'                 =>  array(240, 230, 140),
            'Lavender'              =>  array(230, 230, 250),
            'Lavender Blush'        =>  array(255, 240, 245),
            'Lawn Green'            =>  array(124, 252, 0),
            'Lemon Chiffon'         =>  array(255, 250, 205),
            'Light Blue'            =>  array(173, 216, 230),
            'Light Coral'           =>  array(240, 128, 128),
            'Light Cyan'            =>  array(224, 255, 255),
            'Light Goldenrod Yellow'=>  array(250, 250, 210),
            'Light Gray'            =>  array(211, 211, 211),
            'Light Green'           =>  array(144, 238, 144),
            'Light Pink'            =>  array(255, 182, 193),
            'Light Salmon'          =>  array(255, 160, 122),
            'Light Sea Green'       =>  array(32, 178, 170),
            'Light Sky Blue'        =>  array(135, 206, 250),
            'Light Slate Gray'      =>  array(119, 136, 153),
            'Light Steel Blue'      =>  array(176, 196, 222),
            'Light Yellow'          =>  array(255, 255, 224),
            'Lime'                  =>  array(0, 255, 0),
            'Lime Green'            =>  array(50, 205, 50),
            'Linen'                 =>  array(250, 240, 230),
            'magenta'               =>  array(255, 0, 255),     //  ****d
            'Maroon'                =>  array(128, 0, 0),
            'Medium Aquamarine'     =>  array(102, 205, 170),
            'Medium Blue'           =>  array(0, 0, 205),
            'Medium Orchid'         =>  array(186, 85, 211),
            'Medium Purple'         =>  array(147, 112, 219),
            'Medium Sea Green'      =>  array(60, 179, 113),
            'Medium Slate Blue'     =>  array(123, 104, 238),
            'Medium Spring Green'   =>  array(0, 250, 154),
            'Medium Turquoise'      =>  array(72, 209, 204),
            'Medium Violet Red'     =>  array(199, 21, 133),
            'Midnight Blue'         =>  array(25, 25, 112),
            'Mint Cream'            =>  array(245, 255, 250),
            'Misty Rose'            =>  array(255, 228, 225),
            'Moccasin'              =>  array(255, 228, 181),
            'Navajo White'          =>  array(255, 222, 173),
            'Navy'                  =>  array(0, 0, 128),
            'Old Lace'              =>  array(253, 245, 230),
            'olive'                 =>  array(128, 128, 0), // ****
            'Olive Drab'            =>  array(107, 142, 35),
            'Orange'                =>  array(255, 165, 0),
            'Orange Red'            =>  array(255, 69, 0),
            'Orchid'                =>  array(218, 112, 214),
            'Pale Goldenrod'        =>  array(238, 232, 170),
            'Pale Green'            =>  array(152, 251, 152),
            'Pale Turquoise'        =>  array(175, 238, 238),
            'Pale Violet Red'       =>  array(219, 112, 147),
            'Papaya Whip'           =>  array(255, 239, 213),
            'Peach Puff'            =>  array(255, 218, 185),
            'Peru'                  =>  array(205, 133, 63),
            'Pink'                  =>  array(255, 192, 203),
            'Plum'                  =>  array(221, 160, 221),
            'Powder Blue'           =>  array(176, 224, 230),
            'Purple'                =>  array(128, 0, 128),
            'Red'                   =>  array(255, 0, 0),
            'Rosy Brown'            =>  array(188, 143, 143),
            'Royal Blue'            =>  array(65, 105, 225),
            'Saddle Brown'          =>  array(139, 69, 19),
            'Salmon'                =>  array(250, 128, 114),
            'Sandy Brown'           =>  array(244, 164, 96),
            'Sea Green'             =>  array(46, 139, 87),
            'Seashell'              =>  array(255, 245, 238),
            'Sienna'                =>  array(160, 82, 45),
            'Silver'                =>  array(192, 192, 192),
            'Sky Blue'              =>  array(135, 206, 235),
            'Slate Blue'            =>  array(106, 90, 205),
            'Slate Gray'            =>  array(112, 128, 144),
            'Snow'                  =>  array(255, 250, 250),
            'Spring Green'          =>  array(0, 255, 127),
            'Steel Blue'            =>  array(70, 130, 180),
            'Tan'                   =>  array(210, 180, 140),
            'teal'                  =>  array(0, 128, 128),
            'Thistle'               =>  array(216, 191, 216),
            'Tomato'                =>  array(255, 99, 71),
            'Turquoise'             =>  array(64, 224, 208),
            'Violet'                =>  array(238, 130, 238),
            'Wheat'                 =>  array(245, 222, 179),
            'White'                 =>  array(255, 255, 255),
            'White Smoke'           =>  array(245, 245, 245),
            'Yellow'                =>  array(255, 255, 0),
            'Yellow Green'          =>  array(154, 205, 50)
    );
    foreach($arr_colors as $key=>$color) {
        // $diff = abs($arr_input[0] - $color[0]) + abs($arr_input[1] - $color[1]) + abs($arr_input[2] - $color[2]);

        // Your code looks right, I ran it and got silver. However, I also tried squaring the differences in the components and got (what I thought was) an even better answer: lichtsteelblue. Here's the code I used:

        $diff = pow($arr_input[0] - $color[0],2) + pow($arr_input[1] - $color[1], 2) + pow($arr_input[2] - $color[2], 2);

        $array[$key] = $diff;
    }
    asort($array);
    reset($array);

    list($key, $diff) = each($array);
    return $key;
}

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

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

发布评论

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

评论(2

夏の忆 2024-12-02 07:14:51

带有代码 (100,76,66) 的棕色在哪里。如果搜索不成功,您也不会返回错误的陈述

Where is the Brown colour with the code (100,76,66). Also you dont have and return false statements if the search is unsuccessfull

岁吢 2024-12-02 07:14:51

问题是假设

pow($arr_input[0] - $color[0],2) + pow($arr_input[1] - $color[1], 2) + pow($arr_input[2] - $color[2], 2) lesser

=>颜色更像是

Issue is assumption that

pow($arr_input[0] - $color[0],2) + pow($arr_input[1] - $color[1], 2) + pow($arr_input[2] - $color[2], 2) lesser

=> colors is more like

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