How to match colors?

Hex Colors

Examples: #F0F0F0 and #333

Regex: ^#([a-f0-9]{6}|[a-f0-9]{3})\b$

Hex Colors with Alpha

Examples: #56ff0077

Regex: ^#([a-f0-9]{8}|[a-f0-9]{4})\b$

RGB Colors

Examples: rgb(0,0,0)

Regex: ^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$

RGB Colors with Alpha

Examples: rgba(0,0,0, 0.6)

Regex: ^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d*(?:\.\d+)?)\)$

HSL Colors

Examples: hsl(0,5%,1%)

Regex: ^hsl\(\s*(\d+)\s*,\s*(\d*(?:\.\d+)?%)\s*,\s*(\d*(?:\.\d+)?%)\)$

HSL Colors with Alpha

Examples: hsla(0, 0%, 1%, .9)

Regex: ^hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%,\s*(\d*(?:\.\d+)?)\)$



×

Subscribe

The latest tutorials sent straight to your inbox.