daily pastebin goal
76%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open Belt;
  2.  
  3. [@bs.val]
  4. external unsafeParseInt: (string, int) => Js.Null.t(float) = "parseInt";
  5.  
  6. [@bs.val] [@bs.scope "Math"] external floor: float => int = "floor";
  7.  
  8. type result('a, 'b) =
  9.   | Ok('a)
  10.   | Error('b);
  11.  
  12. let explode = input => input |> Js.String.split("") |> List.fromArray;
  13.  
  14. let stringToIntSafe = (~radix=10, string) =>
  15.   switch (Js.Null.toOption(unsafeParseInt(string, radix))) {
  16.   | None => None
  17.   | Some(x) =>
  18.     switch (x->Js.Float.isNaN) {
  19.     | false => Some(x->int_of_float)
  20.     | _ => None
  21.     }
  22.   };
  23.  
  24. module Color = {
  25.   type t = {
  26.     red: int,
  27.     green: int,
  28.     blue: int,
  29.     alpha: float,
  30.   };
  31.  
  32.   let toRgb = ({red, green, blue}: t) => (red, green, blue);
  33.  
  34.   let toRgba = ({red, green, blue, alpha}: t) => (red, green, blue, alpha);
  35.  
  36.   let toRgbString = ({red, green, blue}: t) => {j|rgb($red, $green, $blue)|j};
  37.  
  38.   let toRgbaString = ({red, green, blue, alpha}: t) => {j|rgba($red, $green, $blue, $alpha)|j};
  39.  
  40.   let toHexString = ({red, green, blue}: t) => {
  41.     let r = Js.Int.toStringWithRadix(~radix=16, red);
  42.     let r = r->Js.String.length == 1 ? "0" ++ r : r;
  43.     let g = Js.Int.toStringWithRadix(~radix=16, green);
  44.     let g = g->Js.String.length == 1 ? "0" ++ g : g;
  45.     let b = Js.Int.toStringWithRadix(~radix=16, blue);
  46.     let b = b->Js.String.length == 1 ? "0" ++ b : b;
  47.     "#" ++ r ++ g ++ b;
  48.   };
  49.  
  50.   let toHexAlphaString = color => {
  51.     let hex = toHexString(color);
  52.     let alpha = (color.alpha *. 255.0)->floor;
  53.     let a = Js.Int.toStringWithRadix(~radix=16, alpha);
  54.     hex ++ a;
  55.   };
  56.  
  57.   let fromRgb = ((red, green, blue)) => {red, green, blue, alpha: 1.0};
  58.  
  59.   let fromRgba = ((red, green, blue, alpha)) => {red, green, blue, alpha};
  60.  
  61.   let fromHexString = string =>
  62.     switch (explode(string)) {
  63.     | ["#", r, g, b] =>
  64.       switch (
  65.         stringToIntSafe(~radix=16, r ++ r),
  66.         stringToIntSafe(~radix=16, g ++ g),
  67.         stringToIntSafe(~radix=16, b ++ b),
  68.       ) {
  69.       | (Some(red), Some(green), Some(blue)) =>
  70.         Ok({red, green, blue, alpha: 1.0})
  71.       | _ => Error("Invalid string provided.")
  72.       }
  73.     | ["#", r, g, b, a] =>
  74.       switch (
  75.         stringToIntSafe(~radix=16, r ++ r),
  76.         stringToIntSafe(~radix=16, g ++ g),
  77.         stringToIntSafe(~radix=16, b ++ b),
  78.         stringToIntSafe(~radix=16, a ++ a),
  79.       ) {
  80.       | (Some(red), Some(green), Some(blue), Some(alpha)) =>
  81.         Ok({red, green, blue, alpha: alpha->float_of_int /. 255.0})
  82.       | _ => Error("Invalid string provided.")
  83.       }
  84.     | ["#", r1, r2, g1, g2, b1, b2] =>
  85.       switch (
  86.         stringToIntSafe(~radix=16, r1 ++ r2),
  87.         stringToIntSafe(~radix=16, g1 ++ g2),
  88.         stringToIntSafe(~radix=16, b1 ++ b2),
  89.       ) {
  90.       | (Some(red), Some(green), Some(blue)) =>
  91.         Ok({red, green, blue, alpha: 1.0})
  92.       | _ => Error("Invalid string provided.")
  93.       }
  94.     | ["#", r1, r2, g1, g2, b1, b2, a1, a2] =>
  95.       switch (
  96.         stringToIntSafe(~radix=16, r1 ++ r2),
  97.         stringToIntSafe(~radix=16, g1 ++ g2),
  98.         stringToIntSafe(~radix=16, b1 ++ b2),
  99.         stringToIntSafe(~radix=16, a1 ++ a2),
  100.       ) {
  101.       | (Some(red), Some(green), Some(blue), Some(alpha)) =>
  102.         Ok({red, green, blue, alpha: alpha->float_of_int /. 255.0})
  103.       | _ => Error("Invalid string provided.")
  104.       }
  105.     | _ => Error("Invalid string provided.")
  106.     };
  107. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top