Created
November 2, 2016 04:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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