Jump to content

Wikifunctions:Type proposals/RGBA color

From Wikifunctions

Summary

Having a format for colors would be nice - as they already have functions dedicated to them (opposite colour (Z13023), Mix colours (Z12997), and colour contrast ratio (Z13028), just to name a few.) However, all of these work either off of hex codes or natural number values - which isn't always the case. This proposal would use a range of 0-1 using Wikifunctions:Type_proposals/Rational_number for red, green, blue, and (optionally) alpha, to increase flexibility.

Uses

This would allow for more specific typing, as well as allowing uses outside of the 8-bit RGB color space.

Structure

The structure is simply an object with 4 keys - for the red, green, blue, and alpha fields - with a rational number inside each.

Example values

Value #0080ff

{
  "type": "RGBA color",
  "red": {
    "type": "rational number",
    "numerator": {
      "type": "integer",
      "sign": "neutral",
      "absolute value": {
        "type": "natural number",
        "value": "0"
      }
    }
    "denominator": {
      "type": "natural number",
      "value": "1"
    }
  },
  "green": {
    "type": "rational number",
    "numerator": {
      "type": "integer",
      "sign": "neutral",
      "absolute value": {
        "type": "natural number",
        "value": "128"
      }
    }
    "denominator": {
      "type": "natural number",
      "value": "255"
    }
  },
  "blue": {
    "type": "rational number",
    "numerator": {
      "type": "integer",
      "sign": "neutral",
      "absolute value": {
        "type": "natural number",
        "value": "1"
      }
    }
    "denominator": {
      "type": "natural number",
      "value": "1"
    }
  },
  "alpha": {
    "type": "rational number",
    "numerator": {
      "type": "integer",
      "sign": "neutral",
      "absolute value": {
        "type": "natural number",
        "value": "1"
      }
    }
    "denominator": {
      "type": "natural number",
      "value": "1"
    }
  }
}
{
  "Z1K1": "Zccccc",
  "ZcccccK1": {
    "Z1K1": "Zmmmmm",
    "ZmmmmmK1": {
      "Z1K1": "Z16683",
      "Z16683K1": "Z16661",
      "Z16683K2": {
        "Z1K1": "Z13518",
        "Z13518K1": "0"
      }
    }
    "ZmmmmmK2": {
      "Z1K1": "Z13518",
      "Z13518K1": "1"
    }
  },
  "ZcccccK2": {
    "Z1K1": "Zmmmmm",
    "ZmmmmmK1": {
      "Z1K1": "Z16683",
      "Z16683K1": "Z16661",
      "Z16683K2": {
        "Z1K1": "Z13518",
        "Z13518K1": "128"
      }
    }
    "ZmmmmmK2": {
      "Z1K1": "Z13518",
      "Z13518K1": "255"
    }
  },
  "ZcccccK3": {
    "Z1K1": "Zmmmmm",
    "ZmmmmmK1": {
      "Z1K1": "Z16683",
      "Z16683K1": "Z16661",
      "Z16683K2": {
        "Z1K1": "Z13518",
        "Z13518K1": "1"
      }
    }
    "ZmmmmmK2": {
      "Z1K1": "Z13518",
      "Z13518K1": "1"
    }
  },
  "ZcccccK4": {
    "Z1K1": "Zmmmmm",
    "ZmmmmmK1": {
      "Z1K1": "Z16683",
      "Z16683K1": "Z16661",
      "Z16683K2": {
        "Z1K1": "Z13518",
        "Z13518K1": "1"
      }
    }
    "ZmmmmmK2": {
      "Z1K1": "Z13518",
      "Z13518K1": "1"
    }
  }
}

Validator

The validator ensures that the fractions are in the range of 0-1, inclusive.

Identity

Two colors are the same if all keys are the same.

Converting to code

Python

A Tuple of fractions, in RGBA order. For the example value, this would be (Fraction(0, 1), Fraction(128, 255), Fraction(1, 1), Fraction(1, 1)).

JavaScript

An array of fractions, in RGBA order. For the example value, this would be [[0, 1], [128, 255], [1, 1], [1,1]]

Renderer

If the color would fit inside the typical 8-bit color hex format, it can be formatted as #rrggbbaa. Alpha should only be shown if it is not 1 (the default), and digits a-f should be in lowercase. If it doesn't fit in the normal format, it should default to showing the fractions in RGBA order. For example: [1/1, 2/3, 951/1327, 1/1].

Parsers

Parsers should be able to read the same formats the renderer puts out [hex codes or fractional representation].

Alternatives

  1. Instead of rational numbers, float64s could be used. They'd be a bit easier to work with without giving up too much precision.
  2. There are numerous other color standards that could work instead, like HSV or CIELAB.
  3. The alpha channel may not be necessary.
  4. We could store the values as bytes.

Comments