Skip to content

Commit

Permalink
[kowainik#51] implemented rgb and rgbMessage function
Browse files Browse the repository at this point in the history
  • Loading branch information
dariodsa committed Apr 3, 2021
1 parent c3d994f commit 40bc041
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Colourista/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Colourista.IO
, whiteMessage
, magentaMessage
, cyanMessage
, rgbMessage
-- ** Aliases with unicode indicators
, successMessage
, infoMessage
Expand All @@ -35,7 +36,8 @@ module Colourista.IO
#if __GLASGOW_HASKELL__ < 804
import Data.Semigroup (Semigroup (..))
#endif
import Data.Text (Text)
import Data.Maybe (fromJust, isNothing)
import Data.Text (pack, Text)

import Colourista.Mode (HasColourMode)

Expand All @@ -47,6 +49,12 @@ import qualified Colourista.Pure as Colourista
-- Direct IO functions
----------------------------------------------------------------------------

-- | Print 'Text' coloured in specified RGB notaion
rgbMessage :: HasColourMode => String -> Text -> IO ()
rgbMessage colour text | isNothing res = errorMessage $ pack $ "Invalid hex value - " ++ colour
| otherwise = formattedMessage [ fromJust res] text
where res = Colourista.rgb colour

-- | Print 'Text' coloured in 'Colourista.red'.
redMessage :: HasColourMode => Text -> IO ()
redMessage = formattedMessage [Colourista.red]
Expand Down
50 changes: 50 additions & 0 deletions src/Colourista/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Colourista.Pure
, white
, magenta
, cyan
, rgb

-- * Background
, redBg
Expand All @@ -42,14 +43,18 @@ module Colourista.Pure
) where

import Data.ByteString (ByteString)
import Data.Foldable (foldl')
import Data.Int (Int8)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe (fromJust, isNothing, mapMaybe)
import Data.Semigroup (Semigroup (..))
import Data.String (IsString (..))
import Data.Text (Text)
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)


Expand Down Expand Up @@ -142,6 +147,51 @@ cyan = withColourMode $ fromString $ setSGRCode [SetColor Foreground Vivid Cyan]
{-# SPECIALIZE cyan :: HasColourMode => Text #-}
{-# SPECIALIZE cyan :: HasColourMode => ByteString #-}

-- | Code to apply any arbitrary hex color for the terminal output.
rgb :: (HasColourMode, IsString str) => String -> Maybe str
rgb hex | length hex > 6 = Nothing
| length rgbValues /= 3 = Nothing
| otherwise = Just $ withColourMode $ fromString $ setSGRCode [SetRGBColor Foreground (sRGB24 redComponent greenComponent blueComponent)]
where
hexVal = replicate (6 - length hex) '0' ++ hex
rgbValues = map fromIntegral $ mapMaybe hexToInt [ (hexVal !! 0) : [hexVal !! 1]
, (hexVal !! 2) : [hexVal !! 3]
, (hexVal !! 4) : [hexVal !! 5]]

redComponent = rgbValues !! 0
greenComponent = rgbValues !! 1
blueComponent = rgbValues !! 2
hexToInt :: String -> Maybe Int
hexToInt [] = Just 0
hexToInt val | isNothing z || isNothing other = Nothing
| otherwise = Just $ fromJust z + 16 * fromJust other
where
other = hexToInt (init val)
z = case last val of
'0' -> Just 0
'1' -> Just 1
'2' -> Just 2
'3' -> Just 3
'4' -> Just 4
'5' -> Just 5
'6' -> Just 6
'7' -> Just 7
'8' -> Just 8
'9' -> Just 9
'a' -> Just 10
'b' -> Just 11
'c' -> Just 12
'd' -> Just 13
'e' -> Just 14
'f' -> Just 15
'A' -> Just 10
'B' -> Just 11
'C' -> Just 12
'D' -> Just 13
'E' -> Just 14
'F' -> Just 15
_ -> Nothing

----------------------------------------------------------------------------
-- Background
----------------------------------------------------------------------------
Expand Down

0 comments on commit 40bc041

Please sign in to comment.