module Termbox.Internal.Color
  ( -- * Color
    Color (..),
    defaultColor,
    red,
    green,
    yellow,
    blue,
    magenta,
    cyan,
    white,
    bright,
    color,
    gray,

    -- * MaybeColor
    MaybeColor,
    unMaybeColor,
    nothingColor,
    justColor,
  )
where

import Data.Coerce (coerce)
import Data.Word (Word16)
import Termbox.Bindings.Hs

-- | A color.
--
-- There are three classes of colors:
--
-- * Basic named colors and their bright variants, such as 'red' and 'bright' 'blue'.
-- * Miscellaneous colors, such as @'color' 33@.
-- * Monochrome colors that range from black (@'gray' 0@) to white (@'gray' 23@).
newtype Color
  = Color Tb_attrs
  deriving newtype (Color -> Color -> Bool
(Color -> Color -> Bool) -> (Color -> Color -> Bool) -> Eq Color
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Color -> Color -> Bool
== :: Color -> Color -> Bool
$c/= :: Color -> Color -> Bool
/= :: Color -> Color -> Bool
Eq)

-- These intentionally don't use e.g. TB_RED, 'cause they're all off-by-one in TB_OUTPUT_256

defaultColor :: Color
defaultColor :: Color
defaultColor =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
0)

red :: Color
red :: Color
red =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
1)

green :: Color
green :: Color
green =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
2)

yellow :: Color
yellow :: Color
yellow =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
3)

blue :: Color
blue :: Color
blue =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
4)

magenta :: Color
magenta :: Color
magenta =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
5)

cyan :: Color
cyan :: Color
cyan =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
6)

white :: Color
white :: Color
white =
  Tb_attrs -> Color
Color (Word16 -> Tb_attrs
Tb_attrs Word16
7)

-- | Make a basic color bright.
bright :: Color -> Color
bright :: Color -> Color
bright =
  (Word16 -> Word16) -> Color -> Color
forall a b. Coercible a b => a -> b
coerce Word16 -> Word16
bright_

bright_ :: Word16 -> Word16
bright_ :: Word16 -> Word16
bright_ Word16
c
  | Word16
c Word16 -> Word16 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word16
7 = Word16
c Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ Word16
8
  | Bool
otherwise = Word16
c

-- | A miscellaneous color.
--
-- Valid values are in the range @[0, 215]@; values outside of this range are clamped.
color :: Int -> Color
color :: Int -> Color
color =
  (Int -> Word16) -> Int -> Color
forall a b. Coercible a b => a -> b
coerce (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int @Word16 (Int -> Word16) -> (Int -> Int) -> Int -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
16) (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
215)

-- | A monochrome color; black is 0 and white is 23.
--
-- Valid values are in the range @[0, 23]@; values outside of this range are clamped.
gray :: Int -> Color
gray :: Int -> Color
gray =
  (Int -> Word16) -> Int -> Color
forall a b. Coercible a b => a -> b
coerce (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int @Word16 (Int -> Word16) -> (Int -> Int) -> Int -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
232) (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
23)

-- This is a more efficient `Maybe Color`; we represent Nothing by WORD_MAX (which isn't a valid termbox color)
newtype MaybeColor
  = MaybeColor Color
  deriving stock (MaybeColor -> MaybeColor -> Bool
(MaybeColor -> MaybeColor -> Bool)
-> (MaybeColor -> MaybeColor -> Bool) -> Eq MaybeColor
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaybeColor -> MaybeColor -> Bool
== :: MaybeColor -> MaybeColor -> Bool
$c/= :: MaybeColor -> MaybeColor -> Bool
/= :: MaybeColor -> MaybeColor -> Bool
Eq)

unMaybeColor :: MaybeColor -> Tb_attrs
unMaybeColor :: MaybeColor -> Tb_attrs
unMaybeColor MaybeColor
c
  | MaybeColor
c MaybeColor -> MaybeColor -> Bool
forall a. Eq a => a -> a -> Bool
== MaybeColor
nothingColor = Tb_attrs
_TB_DEFAULT
  | Bool
otherwise = forall a b. Coercible a b => a -> b
forall a b. Coercible a b => a -> b
coerce @_ @Tb_attrs MaybeColor
c

nothingColor :: MaybeColor
nothingColor :: MaybeColor
nothingColor =
  forall a b. Coercible a b => a -> b
forall a b. Coercible a b => a -> b
coerce @Word16 Word16
forall a. Bounded a => a
maxBound

justColor :: Color -> MaybeColor
justColor :: Color -> MaybeColor
justColor =
  Color -> MaybeColor
MaybeColor