One-color.js

JS colors made easy

Peter Müller, Web developer @onecom

Introduction

Why do we need it?

Why do we need it?

?

But first...

A little domain knowledge

Displays

RGB

HSV

HSL

CSS syntaxes

Specifications

Options

Ext JS

Micro library

Lack of data model

  • "#000000"
  • [0, 0, 0]
  • { R: 0, G: 0, B: 0 }
  • { space: 'rgb', R: 0, G: 0, B: 0 }

Conversion methods

  • fromHexToRGB()
  • fromRGBToHex()
  • fromHexToHSV()
  • fromRGBToHSV()
  • fromHSVToHex()
  • fromHSVToRGB()
  • fromHexToHSL()
  • fromRGBToHSL()
  • fromHSVToHSL()
  • fromHSLToHex()
  • fromHSLToRGB()
  • fromHSLToHSV()
  • RGB and HEX. Simple
  • Lets add HSV
  • Now HSL... You see where this is going
  • Lots of methods, lots of bytes, lots of mental maintenance

Chained modifiers

  • Object with conversion methods
  • Object with channel modifier methods

Color representation

  • Red: [0;255]
  • Green: [0;255]
  • Blue: [0;255]
  • Hue: [0;360]
  • Saturation: [0;100]
  • Value: [0;100]
  • Lightness: [0;100]

Color math: [0;1]

  • RGB: Integers
  • HSL/HSVL: Floating point? Browsers don't get it
  • Math: Infinetly precise → Aproximate by using floating point
  • Other libs only think one conversion at a time

And the winner is...

  • None :(

... but take a look at https://github.com/harthur/color, lots of good ideas!

One-color.js

Specifications

Base

one.color = { // General namespace for One.com libs
    color = function( one.color.* | Array | String | Int ), // Parser
};
one.color.installColorSpace: function ( str space, [str channelNames], obj methods )
one.color.installColorSpace("RGB", "Red", "Green", "Blue", "Alpha", {...});
one.color.RGB  = function ( [0;1], [0;1], [0;1], [0;1] ) // Constructor

And with plugins:

one.color.HSV  = function ( [0;1], [0;1], [0;1], [0;1] ) // Constructor
one.color.HSL  = function ( [0;1], [0;1], [0;1], [0;1] ) // Constructor
one.color.CMYK = function ( [0;1], [0;1], [0;1], [0;1] ) // Constructor

installColorSpace

Metaprogramming FTW!

Methods added to all installed one.color.* prototypes:

installColorSpace

How to write a colorSpace plugin

one.color.installColorSpace('HSV', ['Hue', 'Saturation', 'Value', 'Alpha'], {
    fromRgb: function () { // Becomes one.color.RGB.prototype.toHSV
        // SNIP! Colormath is boring, lets go shopping!
        return new one.color.HSV(h, s, v, this.alpha);
    },

    rgb: function () {
        // SNIP! Colormath... Go read wikipedia
        return new one.color.RGB(r, g, b, this.alpha);
    },

    hsl: function () {
        // SNIP! Colormath... Just copy/paste from someone else
        return new one.color.HSL(this.h, s, l, this.alpha);
    }
});

API usage

Color space conversions

one.color("#99FFFF")
    .hsv()
    .hsl()
    .cmyk()
    .hex(); → "#99FFFF"

Conversions are implicit, why bother...

API usage

Channel manipulation methods

one.color("#99FFFF")
    .red(0.5)
    .hue(-0.2, true)
    .magenta(0)
    .hex(); → "#99ff80"

Future possibilities

FAQ

Based on the rewritten API version on the 'newapi' branch

DEMOTIME!

E
     .  .     ..     . .        ..     .  .
RGB

HSV

CMYK

The CSS NYAN CAT was created by Michal Budzynski for Mozilla Demo Party in Helsinki June 2011

Sound was disabled for the audiences/visitors pleasure...

Questions?