{- |
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 :: Bool -> Colour -> Text
colourHex Bool
dark Colour
c = case Colour
c of
  TypeColour AccountType
at Int
k -> Double -> Text -> Text
tint (Bool -> Int -> Double
tintFraction Bool
dark Int
k) (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Text, Text) -> Text
forall {b}. (b, b) -> b
pick ((Text, Text) -> Text) -> (Text, Text) -> Text
forall a b. (a -> b) -> a -> b
$ AccountType -> (Text, Text)
typeColour (AccountType -> (Text, Text)) -> AccountType -> (Text, Text)
forall a b. (a -> b) -> a -> b
$ AccountType -> AccountType
mainAccountType AccountType
at
  IndexColour Int
k   -> (Text, Text) -> Text
forall {b}. (b, b) -> b
pick ((Text, Text) -> Text) -> (Text, Text) -> Text
forall a b. (a -> b) -> a -> b
$ (Text, Text) -> Maybe (Text, Text) -> (Text, Text)
forall a. a -> Maybe a -> a
fromMaybe (Text
"#2a78d6", Text
"#3987e5") (Maybe (Text, Text) -> (Text, Text))
-> Maybe (Text, Text) -> (Text, Text)
forall a b. (a -> b) -> a -> b
$ [(Text, Text)]
categorical [(Text, Text)] -> Int -> Maybe (Text, Text)
forall a. [a] -> Int -> Maybe a
`atMay` (Int
k Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` [(Text, Text)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Text, Text)]
categorical)
  OtherColour Maybe AccountType
Nothing   -> Text
grey
  OtherColour (Just AccountType
at) -> Double -> Text -> Text -> Text
mixWith Double
0.6 Text
grey (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Text, Text) -> Text
forall {b}. (b, b) -> b
pick ((Text, Text) -> Text) -> (Text, Text) -> Text
forall a b. (a -> b) -> a -> b
$ AccountType -> (Text, Text)
typeColour (AccountType -> (Text, Text)) -> AccountType -> (Text, Text)
forall a b. (a -> b) -> a -> b
$ AccountType -> AccountType
mainAccountType AccountType
at  -- the type's hue, greyed
  Colour
LoneColour      -> (Text, Text) -> Text
forall {b}. (b, b) -> b
pick (Text
"#1597b0", Text
"#22a6be")  -- cyan
  Colour
NeutralColour   -> (Text, Text) -> Text
forall {b}. (b, b) -> b
pick (Text
"#52514e", Text
"#c3c2b7")
  Colour
TotalColour     -> (Text, Text) -> Text
forall {b}. (b, b) -> b
pick (Text
"#0b0b0b", Text
"#ffffff")
  where
    pick :: (b, b) -> b
pick (b
light, b
darkc) = if Bool
dark then b
darkc else b
light
    grey :: Text
grey = Text
"#898781"

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

-- | The categorical palette (light, dark), in its validated order.
categorical :: [(Text, Text)]
categorical :: [(Text, Text)]
categorical =
  [ (Text
"#2a78d6", Text
"#3987e5"), (Text
"#eb6834", Text
"#d95926"), (Text
"#1baf7a", Text
"#199e70"), (Text
"#eda100", Text
"#c98500")
  , (Text
"#e87ba4", Text
"#d55181"), (Text
"#008300", Text
"#008300"), (Text
"#4a3aa7", Text
"#9085e9"), (Text
"#e34948", Text
"#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 :: Bool -> Int -> Double
tintFraction Bool
dark Int
k = Double -> Maybe Double -> Double
forall a. a -> Maybe a -> a
fromMaybe Double
0 (Maybe Double -> Double) -> Maybe Double -> Double
forall a b. (a -> b) -> a -> b
$ Int -> [Double] -> [Double]
forall a. Int -> [a] -> [a]
take Int
maxTints [Double]
fractions [Double] -> Int -> Maybe Double
forall a. [a] -> Int -> Maybe a
`atMay` (Int
k Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
maxTints)
  where fractions :: [Double]
fractions = if Bool
dark then [Double
0, Double
0.4, -Double
0.3, Double
0.65, Double
0.2, -Double
0.15, Double
0.8, -Double
0.45] else [Double
0, -Double
0.3, Double
0.4, Double
0.65, -Double
0.15, Double
0.2, -Double
0.45, Double
0.8]

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

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

tests_Hledger_Write_Chart_Palette :: TestTree
tests_Hledger_Write_Chart_Palette :: TestTree
tests_Hledger_Write_Chart_Palette = String -> [TestTree] -> TestTree
testGroup String
"Write.Chart.Palette" [
   String -> Assertion -> TestTree
testCase String
"colourHex" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     Bool -> Colour -> Text
colourHex Bool
False (AccountType -> Int -> Colour
TypeColour AccountType
Revenue Int
0) Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"#008300"
     Bool -> Colour -> Text
colourHex Bool
True (AccountType -> Int -> Colour
TypeColour AccountType
Expense Int
0) Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"#e66767"
     Bool -> Colour -> Text
colourHex Bool
False (AccountType -> Int -> Colour
TypeColour AccountType
Cash Int
0) Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"#2a78d6"  -- a subtype gets its main type's colour
     Bool -> Colour -> Text
colourHex Bool
False (AccountType -> Int -> Colour
TypeColour AccountType
Expense Int
maxTints) Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool -> Colour -> Text
colourHex Bool
False (AccountType -> Int -> Colour
TypeColour AccountType
Expense Int
0)  -- tints repeat
  ,String -> Assertion -> TestTree
testCase String
"tint" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     Double -> Text -> Text
tint Double
0.5 Text
"#000000" Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"#808080"
     Double -> Text -> Text
tint (-Double
0.5) Text
"#ffffff" Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"#808080"
     Double -> Text -> Text
tint Double
0.5 Text
"not a colour" Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"not a colour"
  ]