-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type t = RGB of int * int * int | ||
|
||
let to_255 str = | ||
match int_of_string_opt ("0x" ^ str) with | ||
| None -> 0 | ||
| Some c -> c | ||
|
||
let rgb r g b = RGB (to_255 r, to_255 g, to_255 b) | ||
|
||
let rgb str = | ||
match String.to_seq str |> List.of_seq |> List.map (String.make 1) with | ||
| [ "#"; r1; r2; g1; g2; b1; b2 ] -> rgb (r1 ^ r2) (g1 ^ g2) (b1 ^ b2) | ||
| [ "#"; r1; g1; b1 ] -> rgb r1 g1 b1 | ||
| _ -> RGB (0, 0, 0) | ||
|
||
let of_hex str = | ||
if String.starts_with ~prefix:"#" str then rgb str else rgb ("#" ^ str) | ||
|
||
(* Calculate the luminance of colour. When applied to background colour, this | ||
function could be used to detect the corresponding foreground colour. *) | ||
let luminance (RGB (r, g, b)) = | ||
let scale x = float_of_int x /. 255.0 in | ||
let r, g, b = (scale r, scale g, scale b) in | ||
(0.2126 *. r) +. (0.7152 *. g) +. (0.0722 *. b) | ||
|
||
let foreground color = if luminance color < 0.3 then `Light else `Dark | ||
let to_escape_seq (RGB (r, g, b)) = Format.sprintf "2;%d;%d;%d" r g b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(** This module provides utilities to work with colors. *) | ||
|
||
(** An RGB colour code *) | ||
type t | ||
|
||
(** Returns RGB colour from a hex string like "7057f" or "#fbca04". | ||
Returns black colour by default if there's a parsing error. *) | ||
val of_hex : string -> t | ||
|
||
(** This function calculates the luminance of colour and suggests a foreground | ||
colour. *) | ||
val foreground : t -> [ `Light | `Dark ] | ||
|
||
(** Convert code to escape sequence. *) | ||
val to_escape_seq : t -> string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module Color = Color | ||
module Doc = Doc | ||
module Layout = Layout | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters