Skip to content

Instantly share code, notes, and snippets.

@kkruups
Created November 2, 2016 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkruups/0e25792e8898a56dea2f677f0321a074 to your computer and use it in GitHub Desktop.
Save kkruups/0e25792e8898a56dea2f677f0321a074 to your computer and use it in GitHub Desktop.
Elm: My Custom Elm Decimal to Hex Converter for CSS 6 digit RGB Colors
import Html exposing (text, div)
import Html.Attributes as Attributes exposing(style)
import List
import Array
import String
getValue: Int -> Array.Array Char -> Char
getValue =
(\idx array ->
(Maybe.withDefault '?' (Array.get idx array)))
toHex: Int -> String
toHex dec =
let
digitsA = Array.fromList (String.toList "0123456789ABCDEF")
x = dec % 16
rest = dec // 16
in
if rest == 0 then
String.fromChar (getValue x digitsA)
else
(toHex rest) ++ String.fromChar (getValue x digitsA)
makeHex6: Int -> String
makeHex6 num =
let
unformatted_hex = toHex num
len = String.length unformatted_hex
in
case len of
6 ->
"#" ++ unformatted_hex
5 ->
"#0" ++ unformatted_hex
4 ->
"#00" ++ unformatted_hex
3 ->
"#000" ++ unformatted_hex
2 ->
"#0000" ++ unformatted_hex
1 ->
"#00000" ++ unformatted_hex
_ ->
"Not a valid number"
main =
div[Attributes.style [("background-color", makeHex6 500), ("color", "white")] ] [text (makeHex6 500)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment