listTypeofValuesValidator($list, $type= 'rgba|hsla')
  _void= 0;
  for v, i in $list
    if match('\b(' + $type + ')\b', typeof(v)) != null
      _void = _void + 1;

  if length($list) != _void
    error("Unexpected type inside the list.")
    return false;

  return true;

mergeColorMaps($bulma-colors, $custom-colors)
  // We return at least Bulma's hard-coded colors
  $merged-colors = $bulma-colors

  // We want a map as input
  if typeof($custom-colors) == 'object'
    for $name, $components in $custom-colors
      // The color name should be a string
      // and the components either a single color
      // or a colors list with at least one element
      if typeof($name) == 'string' and (typeof($components) == 'ident' or listTypeofValuesValidator($components, 'rgba|hsla')) and length($components) >= 1
        $color-base = null
        $color-invert = null
        $color-light = null
        $color-dark = null
        $value = null

        // The param can either be a single color
        // or a list of 2 colors
        if listTypeofValuesValidator($components, 'rgba|hsla') and length($components) == 1
          $color-base = $components
          $color-invert = findColorInvert($color-base)
          $color-light = findLightColor($color-base)
          $color-dark = findDarkColor($color-base)
        else if listTypeofValuesValidator($components, 'rgba|hsla') and length($components) > 1
          $color-base = $components[0]
          // If Invert, Light and Dark are provided
          if length($components) > 3
            $color-invert = $components[1]
            $color-light = $components[2]
            $color-dark = $components[3]
            // If only Invert and Light are provided
          else if length($components) > 2
            $color-invert = $components[1]
            $color-light = $components[2]
            $color-dark = findDarkColor($color-base)
            // If only Invert is provided
          else
            $color-invert = $components[1]
            $color-light = findLightColor($color-base)
            $color-dark = findDarkColor($color-base)

        $value = {
          '1': $color-base,
          '2': $color-invert,
          '3': $color-light,
          '4': $color-dark
        }

        // We only want to merge the map if the color base is an actual color
        if match('\b(rgba|hsla)\b', typeof($color-base))
          // We merge this colors elements as map with Bulma's colors map
          // (we can override them this way, no multiple definition for the same name)
          // $merged-colors: map_merge($merged-colors, ($name: ($color-base, $color-invert, $color-light, $color-dark)))
          $merged-colors[$name] = $value

  return $merged-colors

powerNumber($number, $exp)
  return $number ** $exp

colorLuminance($color)
  $color-rgb = {'red': red($color),'green': green($color),'blue': blue($color)}
  for $name, $value in $color-rgb
    $adjusted = 0
    $value = $value / 255
    if $value < 0.03928
      $value = $value / 12.92
    else
      $value = ($value + .055) / 1.055
      $value = powerNumber($value, 2)
    $color-rgb[$name] = $value
  return (($color-rgb['red'] * .2126) + ($color-rgb['green'] * .7152) + ($color-rgb['blue'] * .0722))

findColorInvert($color)
  if colorLuminance($color) > 0.55
    return rgba(#000, 0.7)
  else
    return #fff

findLightColor($color)
  if typeof($color) == 'rgba' or typeof($color) == 'hsla'
    $l = 96%
    if lightness($color) > 96%
      $l = lightness($color)
    return lightness($color, $l)
  return $background

findDarkColor($color)
  if typeof($color) == 'rgba' or typeof($color) == 'hsla'
    $base-l = 29%
    $luminance = colorLuminance($color)
    $luminance-delta = (0.53 - $luminance)
    $target-l = round($base-l + ($luminance-delta * 53))
    return lightness($color, max($base-l, $target-l))
  return $text-strong

/* Bif replacements to match the behaviour of Sass */

bulma-stylus-darken($color, $amount)
  return lightness($color, lightness($color) - $amount)

bulma-stylus-lighten($color, $amount)
  return lightness($color, lightness($color) + $amount)

bulma-stylus-saturate($color, $amount)
  return saturation($color, saturation($color) + $amount)
