Browse Source

Add first version of color format conversions

David Peter 1 year ago
parent
commit
34b470f6b7

+ 4 - 0
book/build.py

@@ -255,6 +255,10 @@ list_of_functions(
                 "title": "Temperature conversion",
                 "modules": ["physics::temperature_conversion"],
             },
+            {
+                "title": "Color format conversion",
+                "modules": ["extra::color"],
+            },
         ],
     },
 )

+ 35 - 1
book/src/list-functions-other.md

@@ -1,6 +1,6 @@
 # Other functions
 
-[Error handling](#error-handling) · [Floating point](#floating-point) · [Quantities](#quantities) · [Chemical elements](#chemical-elements) · [Mixed unit conversion](#mixed-unit-conversion) · [Temperature conversion](#temperature-conversion)
+[Error handling](#error-handling) · [Floating point](#floating-point) · [Quantities](#quantities) · [Chemical elements](#chemical-elements) · [Mixed unit conversion](#mixed-unit-conversion) · [Temperature conversion](#temperature-conversion) · [Color format conversion](#color-format-conversion)
 
 ## Error handling
 
@@ -141,3 +141,37 @@ More information [here](https://en.wikipedia.org/wiki/Conversion_of_scales_of_te
 fn fahrenheit(t_kelvin: Temperature) -> Scalar
 ```
 
+## Color format conversion
+
+Defined in: `extra::color`
+
+### `rgb`
+
+```nbt
+fn rgb(red: Scalar, green: Scalar, blue: Scalar) -> Color
+```
+
+### `color`
+
+```nbt
+fn color(rgb_hex: Scalar) -> Color
+```
+
+### `color_rgb`
+
+```nbt
+fn color_rgb(color: Color) -> String
+```
+
+### `color_rgb_float`
+
+```nbt
+fn color_rgb_float(color: Color) -> String
+```
+
+### `color_hex`
+
+```nbt
+fn color_hex(color: Color) -> String
+```
+

+ 29 - 0
examples/tests/color.nbt

@@ -0,0 +1,29 @@
+assert_eq(0x000000 -> color, black)
+assert_eq(0xffffff -> color, white)
+assert_eq(0x123456 -> color, Color { red: 0x12, green: 0x34, blue: 0x56 })
+
+assert_eq(black -> color_rgb, "rgb(0, 0, 0)")
+assert_eq(white -> color_rgb, "rgb(255, 255, 255)")
+assert_eq(red -> color_rgb, "rgb(255, 0, 0)")
+assert_eq(green -> color_rgb, "rgb(0, 255, 0)")
+assert_eq(blue -> color_rgb, "rgb(0, 0, 255)")
+assert_eq(0x123456 -> color -> color_rgb, "rgb(18, 52, 86)")
+
+assert_eq(black -> color_rgb_float, "rgb(0.000, 0.000, 0.000)")
+assert_eq(white -> color_rgb_float, "rgb(1.000, 1.000, 1.000)")
+assert_eq(red -> color_rgb_float, "rgb(1.000, 0.000, 0.000)")
+assert_eq(green -> color_rgb_float, "rgb(0.000, 1.000, 0.000)")
+assert_eq(blue -> color_rgb_float, "rgb(0.000, 0.000, 1.000)")
+assert_eq(0x123456 -> color -> color_rgb_float, "rgb(0.071, 0.204, 0.337)")
+
+assert_eq(black -> color_hex, "#000000")
+assert_eq(white -> color_hex, "#ffffff")
+assert_eq(red -> color_hex, "#ff0000")
+assert_eq(green -> color_hex, "#00ff00")
+assert_eq(blue -> color_hex, "#0000ff")
+assert_eq(0x123456 -> color -> color_hex, "#123456")
+
+# Examples:
+
+assert_eq(rgb(225, 36, 143) -> color_hex, "#e1248f")
+assert_eq(0xe1248f -> color -> color_rgb, "rgb(225, 36, 143)")

+ 39 - 0
numbat/modules/extra/color.nbt

@@ -0,0 +1,39 @@
+use core::scalar
+use core::functions
+use core::strings
+
+struct Color {
+  red: Scalar,
+  green: Scalar,
+  blue: Scalar,
+}
+
+fn rgb(red: Scalar, green: Scalar, blue: Scalar) -> Color =
+  Color { red: red, green: green, blue: blue }
+
+fn color(rgb_hex: Scalar) -> Color =
+  rgb(
+    floor(rgb_hex / 256^2),
+    floor((mod(rgb_hex, 256^2)) / 256),
+    mod(rgb_hex, 256))
+
+fn _color_to_scalar(color: Color) -> Scalar =
+  color.red * 0x010000 + color.green * 0x000100 + color.blue
+
+fn color_rgb(color: Color) -> String =
+  "rgb({color.red}, {color.green}, {color.blue})"
+
+fn color_rgb_float(color: Color) -> String =
+  "rgb({color.red / 255:.3}, {color.green / 255:.3}, {color.blue / 255:.3})"
+
+fn color_hex(color: Color) -> String =
+  str_append("#", str_replace(str_replace("{color -> _color_to_scalar -> hex:>8}", "0x", ""), " ", "0"))
+
+let black: Color = rgb(0, 0, 0)
+let white: Color = rgb(255, 255, 255)
+let red: Color = rgb(255, 0, 0)
+let green: Color = rgb(0, 255, 0)
+let blue: Color = rgb(0, 0, 255)
+let yellow: Color = rgb(255, 255, 0)
+let cyan: Color = rgb(0, 255, 255)
+let magenta: Color = rgb(255, 0, 255)

+ 2 - 0
numbat/modules/prelude.nbt

@@ -42,3 +42,5 @@ use chemistry::elements
 
 use datetime::functions
 use datetime::human
+
+use extra::color