xmeta
5/8/2016 - 7:13 AM

Main.js

foreign import toHex :: Int -> String

data Color = Red | Green | Blue | Rgb {r :: Int, g :: Int, b :: Int}

to_hex :: Color -> String
to_hex x = case x of
     Red   -> "#0xFF0000"
     Green -> "#0x00FF00"
     Blue  -> "#0x0000FF"
     Rgb rgb -> "#" <> toHex(rgb.r) <> toHex(rgb.g) <> toHex(rgb.b)
 
main = do
  log(to_hex(Rgb{r:255,g:255,b:255}))
// module Main
// jshint node: true

"use strict";

// Convert a number between 0 and 255 to a hex value between 00 and ff.
exports.toHex = function(n) {
  var str = n.toString(16);
  if (str.length == 1) {
    return "0" + str;
  }
  return str;
};