Overview

(  ( \/ \/ |_| ( \/
 ) ( /\ /  | | ( /\

A tiny JavaScript utility for creating rgb color strings from arbitrary text input.

Example usage:

$('.label').each ->
  label = $ this
  label.css backgroundColor: sexyhex label.text()

The code:

hexDigits = '0123456789ABCDEF'

hex = (dec) ->
  # Convert a decimal value to hexadecimal (`hex 200` returns `"C8"`).
  hexDigits.charAt(dec >> 4) + hexDigits.charAt(dec & 15)

pastelize = (rgb) ->
  # Pastelize an rgb color value.
  # The result is guaranteed to be between `404040` and `BFBFBF` (inclusive).
  pastel = ''
  for channel in /(..)(..)(..)/.exec(rgb).slice 1
    pastel += hex parseInt(channel, 16) % 128 + 64
  pastel

sexyhex = (text) ->
  # Trim `text` to six characters.
  # Badass by default.
  text = text.substr 0, 6
  text += 'BADA55'.substr text.length

  # Generate a valid rgb color string.
  # Multiplier ensures a better spread.
  rgb = ''
  rgb += hex 1337 * chr.charCodeAt 0 for chr in text.split ''

  # Pastelize to increase sex appeal.
  '#' + pastelize rgb

window?.sexyhex = sexyhex
exports?.sexyhex = sexyhex