2021-12-16 20:03:07 +00:00
|
|
|
module Stylix.Output ( makeOutputTable ) where
|
|
|
|
|
|
|
|
import Data.Colour ( RGB(RGB) )
|
|
|
|
import qualified Data.Vector as V
|
|
|
|
import Data.Word ( Word8 )
|
|
|
|
import Text.JSON ( JSObject, toJSObject )
|
|
|
|
import Text.Printf ( printf )
|
|
|
|
|
2023-07-08 13:28:15 +00:00
|
|
|
toHexNum :: Double -> Word8
|
|
|
|
toHexNum = truncate
|
2021-12-16 20:03:07 +00:00
|
|
|
|
2022-05-02 00:12:27 +00:00
|
|
|
{- |
|
|
|
|
Convert a colour to a hexdecimal string.
|
|
|
|
|
|
|
|
>>> toHex (RGB 255 255 255)
|
2023-07-08 13:28:15 +00:00
|
|
|
"ffffff"
|
2022-05-02 00:12:27 +00:00
|
|
|
-}
|
2023-07-08 13:28:15 +00:00
|
|
|
toHex :: RGB -> String
|
|
|
|
toHex (RGB r g b) = printf "%02x%02x%02x" (toHexNum r) (toHexNum g) (toHexNum b)
|
2022-04-23 09:36:20 +00:00
|
|
|
|
2022-05-02 00:12:27 +00:00
|
|
|
-- | Convert a palette to the JSON format expected by Stylix's NixOS modules.
|
2023-07-08 13:28:15 +00:00
|
|
|
makeOutputTable :: V.Vector RGB -> JSObject String
|
2021-12-16 20:03:07 +00:00
|
|
|
makeOutputTable
|
|
|
|
= toJSObject
|
2022-04-23 09:36:20 +00:00
|
|
|
. V.toList
|
2021-12-16 20:03:07 +00:00
|
|
|
. V.imap (\i c -> (printf "base%02X" i, c))
|
2023-07-08 13:28:15 +00:00
|
|
|
. V.map toHex
|