~alcinnz/Mondrian

a3455462990dddadf8213c9e45a5fe5ec6bee475 — Adrian Cochrane 1 year, 6 months ago 36e8776
Parse background-colour CSS property.
M Mondrian.cabal => Mondrian.cabal +2 -1
@@ 19,7 19,8 @@ extra-source-files:  CHANGELOG.md

library
  exposed-modules:     Graphics.Rendering.Rect.CSS,
                       Graphics.Rendering.Rect.CSS.Colour
                       Graphics.Rendering.Rect.CSS.Colour,
                       Graphics.Rendering.Rect.CSS.Background
  -- other-modules:
  -- other-extensions:
  build-depends:       base >=4.13 && <4.14, stylist-traits >= 0.1.3.1 && < 1,

A lib/Graphics/Rendering/Rect/CSS.hs => lib/Graphics/Rendering/Rect/CSS.hs +21 -0
@@ 0,0 1,21 @@
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
module Graphics.Rendering.Rect.CSS(CSSRect(..)) where

import Stylist (PropertyParser(..))
import Graphics.Rendering.Rect.CSS.Colour (ColourPallet(foreground))
import Graphics.Rendering.Rect.CSS.Background (Backgrounds(..))

data CSSRect = CSSRect { colours :: ColourPallet, backgrounds :: Backgrounds }
colour = foreground . colours

instance PropertyParser CSSRect where
    temp = CSSRect { colours = temp, backgrounds = temp }
    inherit CSSRect {..} = CSSRect { colours = inherit colours, backgrounds = temp }

    shorthand self key value
        | Just _ <- longhand self self key value = [(key, value)]
        | otherwise = []
    longhand parent self key value
        | Just ret <- longhand (colours parent) (colours self) key value =
            Just self { colours = ret }
        | otherwise = Nothing

A lib/Graphics/Rendering/Rect/CSS/Background.hs => lib/Graphics/Rendering/Rect/CSS/Background.hs +26 -0
@@ 0,0 1,26 @@
{-# LANGUAGE OverloadedStrings #-}
module Graphics.Rendering.Rect.CSS.Background (Backgrounds(..)) where

import Stylist (PropertyParser(..), parseUnorderedShorthand)
import Graphics.Rendering.Rect.CSS.Colour (ColourPallet, parseColour)
import Data.Colour (AlphaColour, transparent)

data Backgrounds = Backgrounds {
    pallet :: ColourPallet,
    colour :: AlphaColour Double
}

instance PropertyParser Backgrounds where
    temp = Backgrounds { pallet = temp, colour = transparent }
    inherit _ = temp
    priority _ = []

    longhand _ self@Backgrounds{ pallet = c } "background-color" toks
        | Just ([], val) <- parseColour c toks = Just self { colour = val }
    longhand _ _ _ _ = Nothing

    shorthand self "background" toks = parseUnorderedShorthand self [
        "background-color"
      ] toks
    shorthand self key val | Just _ <- longhand self self key val = [(key, val)]
        | otherwise = []