Adds a color helper
This commit is contained in:
parent
c87af3d10e
commit
8e888cba1f
2 changed files with 84 additions and 1 deletions
83
lib/structs/Color.dart
Normal file
83
lib/structs/Color.dart
Normal file
|
@ -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)';
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
name: libac_dart
|
name: libac_dart
|
||||||
description: "Aria's Creations code library"
|
description: "Aria's Creations code library"
|
||||||
version: 1.2.082224+1532
|
version: 1.2.082324+0758
|
||||||
homepage: "https://zontreck.com"
|
homepage: "https://zontreck.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue