Skip to content

Color Query ​

Query methods only read information and do not modify the instance.

isLight() ​

Determine whether the color is light (perceived brightness > 128).

Returns: boolean

ts
xcolor('#ffffff').isLight()  // true
xcolor('#000000').isLight()  // false
xcolor('#ff0000').isLight()  // false

isDark() ​

Determine whether the color is dark. Mutually exclusive with isLight().

Returns: boolean

ts
xcolor('#000000').isDark()  // true
xcolor('#ffffff').isDark()  // false

isValid() ​

Determine whether the input at creation time was a valid color.

Returns: boolean

ts
xcolor('#ff0000').isValid()  // true
xcolor('rgb(255,0,0)').isValid()  // true
xcolor('not-a-color').isValid()   // false
xcolor().isValid()                // true (omitting the argument uses the valid default black)

INFO

Invalid input does not throw an error. The instance is created normally as black (#000000), and only isValid() returns false.

brightness() ​

Calculate perceived brightness (0–255), based on the W3C weighted formula:

brightness=R×299+G×587+B×1141000

Returns: number (0–255)

ts
xcolor('#ffffff').brightness()  // 255
xcolor('#000000').brightness()  // 0
xcolor('#ff0000').brightness()  // 76.245

luminance() ​

Calculate relative luminance (0–1), based on the WCAG 2.0 specification.

Returns: number (0–1)

ts
xcolor('#ffffff').luminance()  // 1
xcolor('#000000').luminance()  // 0
xcolor('#ff0000').luminance()  // 0.2126

contrast(other) ​

Contrast compares RGB channels and displays a ratio rounded to two decimals. Composite translucent colors onto a known background before evaluating the rendered result.

Calculate the WCAG contrast ratio (1–21) against another color.

ParamTypeDescription
otherXColorInputThe target color to compare against

Returns: number (1–21)

ts
xcolor('#000000').contrast('#ffffff')  // 21
xcolor('#ff0000').contrast('#ffffff')  // 4 (approx.)

WCAG Contrast Standards

  • AA body text: ≥ 4.5
  • AA large text: ≥ 3
  • AAA body text: ≥ 7
  • AAA large text: ≥ 4.5

Use the a11y plugin to directly check isReadable() and select mostReadable() among multiple candidate colors.

equals(other) ​

Returns false when either color is invalid.

Determine whether two colors are the same (based on RGBA values; an alpha difference below 0.01 is treated as equal).

ParamTypeDescription
otherXColorInputThe target to compare against

Returns: boolean

ts
xcolor('#ff0000').equals('rgb(255, 0, 0)')  // true
xcolor('#ff0000').equals('#ff0001')         // false

Released under the MIT License.