Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Elm

Visibility

unlisted

Author

legacy-anonymous

Created

2021-01-26T20:45:21Z

Updated

2021-01-26T20:45:21Z

module Main exposing (..)

import Html exposing (text)
import String
import Char

toHex : Int -> String
toHex n =
    let
        hex = String.toUpper(toRadix n)
    in
        if String.length hex == 1 then
            "0" ++  hex
        else
             hex

toRadix : Int -> String
toRadix n =
    let
        getChr c =
            if c < 10 then
                String.fromInt c
            else
                String.fromChar <| Char.fromCode (87 + c)
    in
        if n < 16 then
            getChr n
        else
            (toRadix (n // 16)) ++ (getChr (modBy 16 n))

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

colorToHex : Color -> String
colorToHex color =
  case color of
      Red   ->
          "#FF0000"
      Green ->
          "#00FF00"
      Blue  ->
          "#0000FF"
      Rgb {r, g, b} ->
          "#" ++ (toHex r) ++ (toHex g) ++ (toHex b)

main = text(colorToHex(Rgb{r = 255, g = 255, b = 255}))
INFO