From 8e888cba1f5dfd67296c14fc797b553f74ddae2c Mon Sep 17 00:00:00 2001 From: zontreck Date: Fri, 23 Aug 2024 07:58:20 -0700 Subject: [PATCH] Adds a color helper --- lib/structs/Color.dart | 83 ++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 lib/structs/Color.dart diff --git a/lib/structs/Color.dart b/lib/structs/Color.dart new file mode 100644 index 0000000..c925040 --- /dev/null +++ b/lib/structs/Color.dart @@ -0,0 +1,83 @@ +import '../utils/Vector3.dart'; + +class Color { + double r; + double g; + double b; + + Color(this.r, this.g, this.b); + + // Parse from a hexadecimal color string (e.g., "#FF5733" or "FF5733") + static Color fromHex(String hex) { + hex = hex.replaceAll('#', ''); + + if (hex.length != 6) { + throw FormatException('Hex color should be 6 characters long.'); + } + + final r = int.parse(hex.substring(0, 2), radix: 16) / 255.0; + final g = int.parse(hex.substring(2, 4), radix: 16) / 255.0; + final b = int.parse(hex.substring(4, 6), radix: 16) / 255.0; + + return Color(r, g, b); + } + + // Parse from a decimal representation (e.g., 16711680 for red) + static Color fromDecimal(int decimal) { + final r = ((decimal >> 16) & 0xFF) / 255.0; + final g = ((decimal >> 8) & 0xFF) / 255.0; + final b = (decimal & 0xFF) / 255.0; + + return Color(r, g, b); + } + + // Parse from a vector with values ranging from 1 to 255 (e.g., [255, 0, 0] for red) + static Color fromVector255(Vector3d vector) { + final r = vector.X / 255.0; + final g = vector.Y / 255.0; + final b = vector.Z / 255.0; + + return Color(r, g, b); + } + + // Parse from a normalized vector with values from 0.0 to 1.0 (e.g., [1.0, 0.0, 0.0] for red) + static Color fromNormalizedVector(Vector3d vector) { + return Color(vector.X, vector.Y, vector.Z); + } + + // Convert to hexadecimal color string (e.g., "#FF5733") + String toHex() { + final rHex = + (r * 255).round().toRadixString(16).padLeft(2, '0').toUpperCase(); + final gHex = + (g * 255).round().toRadixString(16).padLeft(2, '0').toUpperCase(); + final bHex = + (b * 255).round().toRadixString(16).padLeft(2, '0').toUpperCase(); + + return '#$rHex$gHex$bHex'; + } + + // Convert to decimal representation (e.g., 16711680 for red) + int toDecimal() { + final rInt = (r * 255).round(); + final gInt = (g * 255).round(); + final bInt = (b * 255).round(); + + return (rInt << 16) | (gInt << 8) | bInt; + } + + // Convert to vector with values ranging from 1 to 255 (e.g., [255, 0, 0] for red) + Vector3d toVector255() { + return Vector3d(X: r * 255, Y: g * 255, Z: b * 255); + } + + // Convert to normalized vector with values from 0.0 to 1.0 (e.g., [1.0, 0.0, 0.0] for red) + Vector3d toNormalizedVector() { + return Vector3d(X: r, Y: g, Z: b); + } + + @override + String toString() { + return 'Color(r: $r, g: $g, b: $b)'; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index b514264..6e810c9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: libac_dart description: "Aria's Creations code library" -version: 1.2.082224+1532 +version: 1.2.082324+0758 homepage: "https://zontreck.com"