diff --git a/colourista.cabal b/colourista.cabal index bdc9457..992be13 100644 --- a/colourista.cabal +++ b/colourista.cabal @@ -28,6 +28,7 @@ source-repository head common common-options build-depends: base >= 4.10.1.0 && < 4.16 + ghc-options: -Wall -Wcompat -Widentities @@ -73,6 +74,7 @@ library build-depends: ansi-terminal >= 0.10 && < 0.12 , bytestring >= 0.10 && < 0.12 + , colour >=2.1.0 , ghc-prim >= 0.5 && < 0.8 , text ^>= 1.2.3.0 diff --git a/src/Colourista/IO.hs b/src/Colourista/IO.hs index d5232e1..83e9ac2 100644 --- a/src/Colourista/IO.hs +++ b/src/Colourista/IO.hs @@ -19,6 +19,7 @@ module Colourista.IO , whiteMessage , magentaMessage , cyanMessage + , rgbMessage -- ** Aliases with unicode indicators , successMessage , infoMessage @@ -37,6 +38,10 @@ import Data.Semigroup (Semigroup (..)) #endif import Data.Text (Text) +import Data.Word (Word8) +import Colourista.Mode (HasColourMode) + + import qualified Data.Text.IO as TIO import qualified Colourista.Pure as Colourista @@ -45,6 +50,11 @@ import qualified Colourista.Pure as Colourista -- Direct IO functions ---------------------------------------------------------------------------- +-- | Print 'Text' coloured in specified RGB notaion +rgbMessage :: Word8 -> Word8 -> Word8 -> Text -> IO () +rgbMessage red green blue = formattedMessage [ resColor ] + where resColor = Colourista.rgb red green blue + -- | Print 'Text' coloured in 'Colourista.red'. redMessage :: Text -> IO () redMessage = formattedMessage [Colourista.red] diff --git a/src/Colourista/Pure.hs b/src/Colourista/Pure.hs index 10043de..490df22 100644 --- a/src/Colourista/Pure.hs +++ b/src/Colourista/Pure.hs @@ -17,6 +17,7 @@ module Colourista.Pure , white , magenta , cyan + , rgb -- * Background , redBg @@ -41,14 +42,20 @@ module Colourista.Pure ) where import Data.ByteString (ByteString) +import Data.Foldable (foldl') +import Data.Int (Int8) import Data.List.NonEmpty (NonEmpty (..)) import Data.Semigroup (Semigroup (..)) import Data.String (IsString (..)) import Data.Text (Text) +import Data.Word (Word8) import System.Console.ANSI (Color (..), ColorIntensity (Vivid), ConsoleIntensity (BoldIntensity), ConsoleLayer (Background, Foreground), SGR (..), Underlining (..), setSGRCode) +import Data.Colour.SRGB (sRGB24) +import Colourista.Mode (HasColourMode, withColourMode) + {- | General purpose function to format strings with multiple options. If this function takes empty list as an argument, no @@ -139,6 +146,13 @@ cyan = fromString $ setSGRCode [SetColor Foreground Vivid Cyan] {-# SPECIALIZE cyan :: Text #-} {-# SPECIALIZE cyan :: ByteString #-} +-- | Code to apply any arbitrary hex color for the terminal output. +-- +-- >>> rgb 0xff 0x00 0x00 "I am red" +-- >>> rgb 0xff 0xff 0xff "I am white" +rgb :: (HasColourMode, IsString str) => Word8 -> Word8 -> Word8 -> str +rgb r g b = withColourMode $ fromString $ setSGRCode [SetRGBColor Foreground (sRGB24 r g b)] + ---------------------------------------------------------------------------- -- Background ----------------------------------------------------------------------------