Check if a string defines a color

javascript check if string is a color
color picker
check if string is hex javascript
hex color
how to check hex color code is valid or not
html color picker
ddd color picker
check valid hex string online

I'm looking for a way to check if a string defines a color since my program relies on users inputting colors and it breaks when they enter a wrong color. How can I do this? Here are some examples:

check_color("blue") > True
check_color("deep sky blue") > True
check_color("test") > False
check_color("#708090") > True

Here is the way to check if the string defines a color using matplotlib:

>>> from matplotlib.colors import is_color_like
>>>
>>> is_color_like('red')
True
>>> is_color_like('re')
False
>>> is_color_like(0.5)
False
>>> is_color_like('0.5')
True
>>> is_color_like(None)
False
>>>
>>> matplotlib.colors.__file__
'/usr/lib/python2.7/site-packages/matplotlib/colors.py'

In the source code of the matplotlib.colors module it's written:

The module also provides functions for checking whether an object can be interpreted as a color (:func:is_color_like), for converting such an object to an RGBA tuple (:func:to_rgba) or to an HTML-like hex string in the #rrggbb format (:func:to_hex), and a sequence of colors to an (n, 4) RGBA array (:func:to_rgba_array). Caching is used for efficiency.

Javascript - check if string is valid CSS color?, Here's a simple function that checks color name support in the current browser: function isColor(strColor){ var s = new Option().style; s.color  Step 2: Set the border color to an empty string. The code might look something like this: e=document.getElementbyId('mydiv'); e.style.borderColor=""; Step 3: Set the border color to the color you aren’t sure about. e.style.borderColor=testcol; Step 4: Check to see if the color actually got changed. If testcol is invalid, no change will occur.


One possible way might be is to use colour package. If you do not have it install use command pip install colour. Then, you can use following:

from colour import Color

def check_color(color):
    try:
        # Converting 'deep sky blue' to 'deepskyblue'
        color = color.replace(" ", "")
        Color(color)
        # if everything goes fine then return True
        return True
    except ValueError: # The color code was not found
        return False

check_color("blue") 
check_color("deep sky blue")
check_color("test") 
check_color("#708090")

JavaScript, A RegExp is used to check whether the alphabets ranging between A and F, Integers from 0-9 and is of length = 3 or 6. Example 1: In this example, the validity of  A predefined color is also called a known color and is represented by an element of the KnownColor enumeration. If the name parameter is not the valid name of a predefined color, the FromName method creates a Color structure that has an ARGB value of 0 (that is, all ARGB components are 0).


You should use something like this:

import re
VALID_COLORS = ['blue', 'red']
HEX_COLOR_REGEX = r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$'

def is_hex_color(input_string):
    regexp = re.compile(HEX_COLOR_REGEX)
    if regexp.search(input_string):
        return True
    return False

def is_color(input_string):
    for color in VALID_COLORS:
        if color in input_string or is_hex_color(input_string):
            return True
    return False

How to validate Hexadecimal Color Code using Regular Expression , Given a string str, the task is to check whether the string is valid hexadecimal colour code or not by using Regular Expression. The valid hexadecimal color code  The colorcode is part of the string. You will not be able to put a string longer than it on the sign, even if part of it is a color code. Necrodoom , Sep 1, 2014


You can use an external module of colours. Install the colour module with pip:

pip install colour

Then you can do:

>>> from colour import Color
>>> name = "blue"
>>> try:
        Color(name.replace(" ","")) 
        print("Valid Color")
except:
    print("Invalid color")

Since it raises a exception when the colour isn't found.

heres a small documentation of the module

is-color, is-color Build Status. Check if a string is CSS color value. Install. $ npm install is-​color. Usage. var isColor = require('is-color'). isColor('rgb(255  In some cases, you may have to convert from a string to a color type. For example, if you accept user input and the user types the word "white", you can convert this into a real color and use it with the Color.FromName method. Note: This won't work for color names that are not recognized in the list of properties.


is.color function, In fact, if a (k−1)-color is reached by a continuous arrow, it suffices to check requires the computation of the (k−1)-colors of two portions of the string which only  Re: Condition based on cell color - IF cell color red, then. The only way to get today's date (and to have it updated automatically), is to use TODAY(). Other than that, it would need to be entered manually - either by you typing it in, or to have a macro do it for you


Logical Foundations of Computer Science: International Symposium, , In general, node Nj will receive strings of the form p1x1p2x2 . Node Nj will check that if in a candidate solution, vertex j is colored with xj ∈ {c1 ,c 2 ,c3}, then​  clr_* string Functions for each supported color. E.g. clr_white, clr_black See clr_dump for list. fn_exists General bash function to check if string is a valid function


Mechanisms, Symbols, and Models Underlying Cognition: First , Subjects answer were recorded using a microphone to automatically detect the chosen because is perceptually similar to a string of letters composing a color  The most frequent application of Color is to define color-related properties in a UI element as part of a UWP app using C++, C#, or Visual Basic and its XAML UI definition. Various predefined Color values are available as static properties on the Colors class. These properties are useful for setting Color values in code that match the attribute