packages feed

hledger-lib-2.0: Hledger/Write/Chart/Palette.hs

{- |
The exact colours of chart series, shared by the chart renderers (SVG, and text in terminals with 24-bit colour),
so that charts look the same everywhere.

The categorical palette and chart colours are the dataviz skill's validated reference palette (8 slots, light and dark variants);
the account type colours were chosen from it and validated on the pairs that appear together (assets-liabilities-equity, revenues-expenses).
-}

{-# LANGUAGE OverloadedStrings #-}

module Hledger.Write.Chart.Palette (
  colourHex,
  tint,
  tests_Hledger_Write_Chart_Palette,
) where

import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Numeric (readHex)
import Safe (atMay)
import Text.Printf (printf)

import Hledger.Data.Types (AccountType(..))
import Hledger.Utils.Test
import Hledger.Write.Chart (Colour(..), mainAccountType, maxTints)


-- | A chart colour as #rrggbb, in its light variant (for light backgrounds) or its dark variant (for dark backgrounds).
colourHex :: Bool -> Colour -> Text
colourHex dark c = case c of
  TypeColour at k -> tint (tintFraction dark k) $ pick $ typeColour $ mainAccountType at
  IndexColour k   -> pick $ fromMaybe ("#2a78d6", "#3987e5") $ categorical `atMay` (k `mod` length categorical)
  OtherColour Nothing   -> grey
  OtherColour (Just at) -> mixWith 0.6 grey $ pick $ typeColour $ mainAccountType at  -- the type's hue, greyed
  LoneColour      -> pick ("#1597b0", "#22a6be")  -- cyan
  NeutralColour   -> pick ("#52514e", "#c3c2b7")
  TotalColour     -> pick ("#0b0b0b", "#ffffff")
  where
    pick (light, darkc) = if dark then darkc else light
    grey = "#898781"

-- | Conventional colours for the main account types (light, dark).
typeColour :: AccountType -> (Text, Text)
typeColour at = case at of
  Asset     -> ("#2a78d6", "#3987e5")  -- blue
  Liability -> ("#eda100", "#c98500")  -- amber
  Equity    -> ("#4a3aa7", "#9085e9")  -- violet
  Revenue   -> ("#008300", "#008300")  -- green
  Expense   -> ("#e34948", "#e66767")  -- red
  _         -> ("#4a3aa7", "#9085e9")

-- | The categorical palette (light, dark), in its validated order.
categorical :: [(Text, Text)]
categorical =
  [ ("#2a78d6", "#3987e5"), ("#eb6834", "#d95926"), ("#1baf7a", "#199e70"), ("#eda100", "#c98500")
  , ("#e87ba4", "#d55181"), ("#008300", "#008300"), ("#4a3aa7", "#9085e9"), ("#e34948", "#e66767") ]

-- | How the Nth tint of a type's colour differs from it ('maxTints' of them, repeating), on a light or dark background:
-- the fraction to mix with white (positive) or black (negative). Ordered with the most visible first, since accounts prefer
-- the first ones (see 'seriesColours'): the colour itself, then darker (on a light background) or lighter (on a dark one),
-- then the other of those, then lighter still.
tintFraction :: Bool -> Int -> Double
tintFraction dark k = fromMaybe 0 $ take maxTints fractions `atMay` (k `mod` maxTints)
  where fractions = if dark then [0, 0.4, -0.3, 0.65, 0.2, -0.15, 0.8, -0.45] else [0, -0.3, 0.4, 0.65, -0.15, 0.2, -0.45, 0.8]

-- | Mix a #rrggbb colour with white (by a positive fraction) or black (by a negative fraction).
tint :: Double -> Text -> Text
tint f
  | f >= 0    = mixWith f "#ffffff"
  | otherwise = mixWith (negate f) "#000000"

-- | Mix a #rrggbb colour with another, by this fraction of the other (0 to 1).
mixWith :: Double -> Text -> Text -> Text
mixWith f other hex = case (rgb hex, rgb other) of
    (Just cs, Just os) -> "#" <> T.concat (zipWith (\c o -> T.pack $ printf "%02x" (mix c o)) cs os)
    _ -> hex
  where
    rgb h = let digits = T.drop 1 h in
      if T.length digits == 6 then mapM channel [T.take 2 $ T.drop i digits | i <- [0, 2, 4]] else Nothing
    channel t = case readHex (T.unpack t) of [(n, "")] -> Just (n :: Int); _ -> Nothing
    mix :: Int -> Int -> Int
    mix c o = round $ fromIntegral c + (fromIntegral o - fromIntegral c) * f

tests_Hledger_Write_Chart_Palette :: TestTree
tests_Hledger_Write_Chart_Palette = testGroup "Write.Chart.Palette" [
   testCase "colourHex" $ do
     colourHex False (TypeColour Revenue 0) @?= "#008300"
     colourHex True (TypeColour Expense 0) @?= "#e66767"
     colourHex False (TypeColour Cash 0) @?= "#2a78d6"  -- a subtype gets its main type's colour
     colourHex False (TypeColour Expense maxTints) @?= colourHex False (TypeColour Expense 0)  -- tints repeat
  ,testCase "tint" $ do
     tint 0.5 "#000000" @?= "#808080"
     tint (-0.5) "#ffffff" @?= "#808080"
     tint 0.5 "not a colour" @?= "not a colour"
  ]