Jump to content

How to check if a string is a color code


Recommended Posts


I want to check it a string is a color code or not.

I tried with string.match but it's not precise.. It's like using string.find.

So if I have;

local theString = "#FF0000Test"

string.find(theString, "#%x%x%x%x%x%x") will return true, but it's not since the string contains the 'extension' (Test)

I tried if theString == "#%x%x%x%x%x%x" then ...

But it didn't work either..

function isHexCode(theString)

--Which condition to put here?



Link to comment

Well ok

I made this:

It seems to work

function isStringHexCode(theHex) 
    local Hex = theHex:gsub("#", "") 
    if #Hex == 6 and tonumber(Hex, 16) then return true end 
    return false 

I got another question,

How can I check if the first character of the string is "#" or not?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...