about summary refs log tree commit diff stats
path: root/src/js/jscolor.nim
blob: 48f7fb34e0f33ba79930a439d09bdc5cf64757c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import std/strutils

import bindings/quickjs
import js/error
import js/fromjs
import js/javascript
import js/tojs
import types/color
import utils/charcategory
import utils/twtstr

func parseLegacyColor*(s: string): JSResult[RGBColor] =
  if s == "":
    return err(newTypeError("Color value must not be the empty string"))
  let s = s.strip(chars = AsciiWhitespace).toLowerAscii()
  if s == "transparent":
    return err(newTypeError("Color must not be transparent"))
  return ok(parseLegacyColor0(s))

proc toJS*(ctx: JSContext; rgb: RGBColor): JSValue =
  var res = "#"
  res.pushHex(rgb.r)
  res.pushHex(rgb.g)
  res.pushHex(rgb.b)
  return toJS(ctx, res)

proc fromJSRGBColor*(ctx: JSContext; val: JSValue): JSResult[RGBColor] =
  return parseLegacyColor(?fromJS[string](ctx, val))

proc toJS*(ctx: JSContext; rgba: ARGBColor): JSValue =
  var res = "#"
  res.pushHex(rgba.r)
  res.pushHex(rgba.g)
  res.pushHex(rgba.b)
  res.pushHex(rgba.a)
  return toJS(ctx, res)

proc fromJSARGBColor*(ctx: JSContext; val: JSValue): JSResult[ARGBColor] =
  if JS_IsNumber(val):
    # as hex
    return ok(ARGBColor(?fromJS[uint32](ctx, val)))
  # parse
  let x = parseARGBColor(?fromJS[string](ctx, val))
  if x.isSome:
    return ok(x.get)
  return errTypeError("Unrecognized color")