{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]

	* Defines the data-type which represents the physical (rather than logical) colour of the /board/ & of /piece/s.

	* The number of physical colours used to represent the /board/ & /piece/s can be greater than the two /logical colour/s required,
	but is limited in practice, since the terminal (optionally) used to render the image, typically can't cope with with a large number.
-}

module BishBosh.Attribute.PhysicalColour(
-- * Types
-- ** Type-synonyms
--	ANSIColourCode,
-- ** Data-types
	PhysicalColour(..),
-- * Constants
	black,
	red,
	green,
	yellow,
	blue,
	magenta,
	cyan,
	white,
	range,
-- * Functions
--	toANSIColourCode,
	mkFgColourCode,
	mkBgColourCode,
	selectGraphicsRendition,
	bracket
) where

import qualified	Control.DeepSeq
import qualified	Text.XML.HXT.Arrow.Pickle	as HXT
import qualified	Text.XML.HXT.Arrow.Pickle.Schema

-- | Defines the physical colours which can typically be rendered by a terminal.
data PhysicalColour
	= Black
	| Red
	| Green
	| Yellow
	| Blue
	| Magenta
	| Cyan
	| White
	deriving (Int -> PhysicalColour
PhysicalColour -> Int
PhysicalColour -> [PhysicalColour]
PhysicalColour -> PhysicalColour
PhysicalColour -> PhysicalColour -> [PhysicalColour]
PhysicalColour
-> PhysicalColour -> PhysicalColour -> [PhysicalColour]
(PhysicalColour -> PhysicalColour)
-> (PhysicalColour -> PhysicalColour)
-> (Int -> PhysicalColour)
-> (PhysicalColour -> Int)
-> (PhysicalColour -> [PhysicalColour])
-> (PhysicalColour -> PhysicalColour -> [PhysicalColour])
-> (PhysicalColour -> PhysicalColour -> [PhysicalColour])
-> (PhysicalColour
    -> PhysicalColour -> PhysicalColour -> [PhysicalColour])
-> Enum PhysicalColour
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: PhysicalColour
-> PhysicalColour -> PhysicalColour -> [PhysicalColour]
$cenumFromThenTo :: PhysicalColour
-> PhysicalColour -> PhysicalColour -> [PhysicalColour]
enumFromTo :: PhysicalColour -> PhysicalColour -> [PhysicalColour]
$cenumFromTo :: PhysicalColour -> PhysicalColour -> [PhysicalColour]
enumFromThen :: PhysicalColour -> PhysicalColour -> [PhysicalColour]
$cenumFromThen :: PhysicalColour -> PhysicalColour -> [PhysicalColour]
enumFrom :: PhysicalColour -> [PhysicalColour]
$cenumFrom :: PhysicalColour -> [PhysicalColour]
fromEnum :: PhysicalColour -> Int
$cfromEnum :: PhysicalColour -> Int
toEnum :: Int -> PhysicalColour
$ctoEnum :: Int -> PhysicalColour
pred :: PhysicalColour -> PhysicalColour
$cpred :: PhysicalColour -> PhysicalColour
succ :: PhysicalColour -> PhysicalColour
$csucc :: PhysicalColour -> PhysicalColour
Enum, PhysicalColour -> PhysicalColour -> Bool
(PhysicalColour -> PhysicalColour -> Bool)
-> (PhysicalColour -> PhysicalColour -> Bool) -> Eq PhysicalColour
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PhysicalColour -> PhysicalColour -> Bool
$c/= :: PhysicalColour -> PhysicalColour -> Bool
== :: PhysicalColour -> PhysicalColour -> Bool
$c== :: PhysicalColour -> PhysicalColour -> Bool
Eq, ReadPrec [PhysicalColour]
ReadPrec PhysicalColour
Int -> ReadS PhysicalColour
ReadS [PhysicalColour]
(Int -> ReadS PhysicalColour)
-> ReadS [PhysicalColour]
-> ReadPrec PhysicalColour
-> ReadPrec [PhysicalColour]
-> Read PhysicalColour
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PhysicalColour]
$creadListPrec :: ReadPrec [PhysicalColour]
readPrec :: ReadPrec PhysicalColour
$creadPrec :: ReadPrec PhysicalColour
readList :: ReadS [PhysicalColour]
$creadList :: ReadS [PhysicalColour]
readsPrec :: Int -> ReadS PhysicalColour
$creadsPrec :: Int -> ReadS PhysicalColour
Read, Int -> PhysicalColour -> ShowS
[PhysicalColour] -> ShowS
PhysicalColour -> String
(Int -> PhysicalColour -> ShowS)
-> (PhysicalColour -> String)
-> ([PhysicalColour] -> ShowS)
-> Show PhysicalColour
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PhysicalColour] -> ShowS
$cshowList :: [PhysicalColour] -> ShowS
show :: PhysicalColour -> String
$cshow :: PhysicalColour -> String
showsPrec :: Int -> PhysicalColour -> ShowS
$cshowsPrec :: Int -> PhysicalColour -> ShowS
Show)

instance Control.DeepSeq.NFData PhysicalColour where
	rnf :: PhysicalColour -> ()
rnf PhysicalColour
_	= ()

instance Bounded PhysicalColour where
	minBound :: PhysicalColour
minBound	= PhysicalColour
black
	maxBound :: PhysicalColour
maxBound	= PhysicalColour
white

instance HXT.XmlPickler PhysicalColour where
	xpickle :: PU PhysicalColour
xpickle	= (String -> PhysicalColour, PhysicalColour -> String)
-> PU String -> PU PhysicalColour
forall a b. (a -> b, b -> a) -> PU a -> PU b
HXT.xpWrap (String -> PhysicalColour
forall a. Read a => String -> a
read, PhysicalColour -> String
forall a. Show a => a -> String
show) (PU String -> PU PhysicalColour)
-> ([String] -> PU String) -> [String] -> PU PhysicalColour
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Schema -> PU String
HXT.xpTextDT (Schema -> PU String)
-> ([String] -> Schema) -> [String] -> PU String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> Schema
Text.XML.HXT.Arrow.Pickle.Schema.scEnum ([String] -> PU PhysicalColour) -> [String] -> PU PhysicalColour
forall a b. (a -> b) -> a -> b
$ (PhysicalColour -> String) -> [PhysicalColour] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map PhysicalColour -> String
forall a. Show a => a -> String
show [PhysicalColour]
range

-- | Constant.
black :: PhysicalColour
black :: PhysicalColour
black	= PhysicalColour
Black

-- | Constant.
red :: PhysicalColour
red :: PhysicalColour
red	= PhysicalColour
Red

-- | Constant.
green :: PhysicalColour
green :: PhysicalColour
green	= PhysicalColour
Green

-- | Constant.
yellow :: PhysicalColour
yellow :: PhysicalColour
yellow	= PhysicalColour
Yellow

-- | Constant.
blue :: PhysicalColour
blue :: PhysicalColour
blue	= PhysicalColour
Blue

-- | Constant.
magenta :: PhysicalColour
magenta :: PhysicalColour
magenta	= PhysicalColour
Magenta

-- | Constant.
cyan :: PhysicalColour
cyan :: PhysicalColour
cyan	= PhysicalColour
Cyan

-- | Constant.
white :: PhysicalColour
white :: PhysicalColour
white	= PhysicalColour
White

-- | The constant complete range of values.
range :: [PhysicalColour]
range :: [PhysicalColour]
range	= [PhysicalColour
forall a. Bounded a => a
minBound .. PhysicalColour
forall a. Bounded a => a
maxBound]

-- | A colour-code, as used by terminal-emulators; <https://en.wikipedia.org/wiki/ANSI_escape_code>.
type ANSIColourCode	= Int

-- | Offset the specified colour-code, so that it applies to the foreground.
mkFgColourCode :: PhysicalColour -> ANSIColourCode
mkFgColourCode :: PhysicalColour -> Int
mkFgColourCode	= (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
30) (Int -> Int) -> (PhysicalColour -> Int) -> PhysicalColour -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PhysicalColour -> Int
toANSIColourCode

-- | Offset the specified colour-code, so that it applies to the background.
mkBgColourCode :: PhysicalColour -> ANSIColourCode
mkBgColourCode :: PhysicalColour -> Int
mkBgColourCode	= (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
40) (Int -> Int) -> (PhysicalColour -> Int) -> PhysicalColour -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PhysicalColour -> Int
toANSIColourCode

-- | Translate.
toANSIColourCode :: PhysicalColour -> ANSIColourCode
toANSIColourCode :: PhysicalColour -> Int
toANSIColourCode	= PhysicalColour -> Int
forall a. Enum a => a -> Int
fromEnum	-- CAVEAT: the order of the data-constructors has been defined with this in mind.

-- | Generate the escape-sequence required to change a terminal to the specified physical colour.
selectGraphicsRendition :: Bool -> ANSIColourCode -> String
selectGraphicsRendition :: Bool -> Int -> String
selectGraphicsRendition Bool
isBold Int
parameter	= String -> ShowS
showString String
"\x1b[" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
parameter ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (if Bool
isBold then String -> ShowS
showString String
";1" else ShowS
forall a. a -> a
id) String
"m"

-- | Render the specified string according to instructions, then revert to default.
bracket :: String -> String -> ShowS
bracket :: String -> String -> ShowS
bracket String
graphicsRendition String
s	= String -> ShowS
showString String
graphicsRendition ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString (
	Bool -> Int -> String
selectGraphicsRendition Bool
False Int
0
 )