babincc / flutter_workshop

This repo houses add-ons, plug-ins, and helpful code to make Flutter programming easier.
5 stars 1 forks source link

Color calculators #2

Closed babincc closed 1 year ago

babincc commented 1 year ago

Add color calculators/converters

babincc commented 1 year ago

Added in 1.1.0 update

babincc commented 1 year ago

Examples:

ColorConverter.xy2rgb(0.5, 0.5); // [255, 222, 0]
ColorConverter.rgb2hsl(255, 0, 0); // [0.0, 1.0, 0.5]
ColorConverter.hsv2hex(0, 1.0 , 1.0); // "ffff0000"
ColorConverter.color2hsv(Color(0xffff0000)); // [0.0, 1.0, 1.0]
ColorConverter.int2rgb(4286611584); // [128, 128, 128]

Also can be used as an extension of Flutter's Color object

Color myColor = Color(0xff8a4888);

myColor.toXy(); // [0.3209554122773742, 0.21993715851681886, 0.1181557673818057]
myColor.toRgb(); // [138, 72, 136]
myColor.toHex(); // "ff8a4888"
myColor.toInt(); // 4287252616
esDotDev commented 5 months ago

It would be nice if we could go directly from Flutter Color to ColorXY. Something like:

final white = ColorXy.fromColor(Colors.white.withOpacity(.5));
final off = ColorXy.fromColor(Colors.transparent);
babincc commented 5 months ago

Thanks for bringing this up! I have added it with the latest commit 9d7fd88!