-6

I want to convert hex colour code to colour name like

FF0000 -> Red

if you have any idea how it can possible?

Community
  • 1
  • 1
user3737353
  • 1
  • 1
  • 1
  • Show us what have you tried yet . – Shub Aug 02 '14 at 11:01
  • http://codegolf.stackexchange.com/questions/32386/what-colour-is-this/32412#32412 – scrblnrd3 Aug 02 '14 at 11:02
  • [here](http://www.color-hex.com/color-names.html) is a good collection of color names with its hex values you may refer to. – Shub Aug 02 '14 at 11:04
  • There are 16777215 possible HEX colour values. There are not that many named colours... you can approximate the colour's name, but it will never be perfect. – Sverri M. Olsen Aug 02 '14 at 11:06
  • Also, why?! The only reason I can think of is to shave off a couple bytes from filesizes, but is it *really* necessary? – Sverri M. Olsen Aug 02 '14 at 11:15
  • I've downvoted, as I think questions like this need to contain evidence of prior effort. – halfer Aug 02 '14 at 11:37
  • possible duplicate of [convert hex code to color name](http://stackoverflow.com/questions/7791710/convert-hex-code-to-color-name) – halfer Aug 02 '14 at 11:39
  • Just make an array with hex `'hex values' => 'colour name'`. There is only a limited number of cases this would actually be usefull though... It would have helped if you stated why you wanted to do this. – Ties Aug 02 '14 at 11:47

3 Answers3

1

Using HTML and JavaScript, see the code :

<html>
<head>
<script type="text/javascript" src="http://chir.ag/projects/ntc/ntc.js"></script>
<script>
var result = {
    r : function(hex)
    {
        var color = ntc.name(hex);
        document.getElementsByName("namecolor")[0].value = color[1];
    }
}
</script>
</head>
<body>
HEX CODE <input type="text" name="colorhex" value="ff0000" onKeyup="result.r(this.value)"><br>
COLOR NAME <input type="text" name="namecolor">
</body>
</html>

Source of the Javascript code project (and php inspired by) : http://chir.ag/projects/name-that-color/

Using PHP : This exemple a simple strict name => value, maybe can be completed by using the same method of javascript code (like delta).

<?php
$hexcolor = array(
    '000000' => 'Black',
    '000080' => 'Navy Blue',
    '0000C8' => 'Dark Blue',
    '0000FF' => 'Blue'); // Must be completed !

echo $hexcolor('000080'); // Return the strict color name
?>

low

low
  • 49
  • 5
1

in your DB You can create an table with 2 columns one for color name and hex code and the whenever then you can easily get color name by its hex value if exist. You can get an collection of color hex values with its name from here.

Well there is no predefined function in php and its not possible to logically generate color names without a database.

Shub
  • 2,686
  • 17
  • 26
0

how ever it is difficult get color name exactly the code because combination of code is so much and the natural colors are bit low which have name. but you can get the exact color which code you have entered by this code

this is not related to this question this is an example for helping purpose :

<?php
$color = "00000";
if(isset($_REQUEST['color']))
{
    $color = trim($_REQUEST['color']);
}
?>
<form method="post" action="">
    <table>
    <tr>
        <td>Color Code : </td>
        <td><input type="text" name="color" maxlength="6" value="<?php echo $color;?>"/></td>
        <td><input type="submit" name="submit" value="go"/></td>
    </tr>
    </table>
</form>
<div style="background-color:<?php echo $color;?>; width:100px; height:50px;"></div>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • I don't think this answers the question - the OP wants to convert hex colours to English colour names. – halfer Aug 02 '14 at 11:12