Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace HslColor for HSLColor from painting #18

Merged
merged 14 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/src/conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ HslColor rgbToHsl({required double r, required double g, required double b}) {
}

h /= 6.0;
return HslColor(h: h, s: s, l: l);
return HSLColor.fromAHSL(1.0, h, s, l);
}

Color hslToColor(HslColor hsl) {
return hslToRgb(hsl);
}

Color hslToRgb(HslColor hsl) {
Color hslToRgb(HSLColor hsl) {
double r;
double g;
double b;
final double h = bound01(hsl.h, 360.0);
final double s = bound01(hsl.s * 100, 100.0);
final double l = bound01(hsl.l * 100, 100.0);
final double h = bound01(hsl.hue, 360.0);
final double s = bound01(hsl.saturation * 100, 100.0);
final double l = bound01(hsl.lightness * 100, 100.0);

if (s == 0.0) {
r = g = b = l;
Expand All @@ -57,7 +53,7 @@ Color hslToRgb(HslColor hsl) {
b = _hue2rgb(p, q, h - 1 / 3);
}
return Color.fromARGB(
hsl.a.round(), (r * 255).round(), (g * 255).round(), (b * 255).round());
hsl.alpha.round(), (r * 255).round(), (g * 255).round(), (b * 255).round());
}

HSVColor colorToHsv(Color color) {
Expand Down
12 changes: 0 additions & 12 deletions lib/src/hsl_color.dart

This file was deleted.

45 changes: 16 additions & 29 deletions lib/src/tinycolor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import 'package:flutter/painting.dart';
import 'package:pigment/pigment.dart';

import 'conversion.dart';
import 'hsl_color.dart';
import 'util.dart';

export 'hsl_color.dart';
export 'color_extension.dart';

class TinyColor {
Expand All @@ -24,8 +22,8 @@ class TinyColor {
return TinyColor(Color.fromARGB(a, r, g, b));
}

factory TinyColor.fromHSL(HslColor hsl) {
return TinyColor(hslToColor(hsl));
factory TinyColor.fromHSL(HSLColor hsl) {
return TinyColor(hsl.toColor());
}

factory TinyColor.fromHSV(HSVColor hsv) {
Expand Down Expand Up @@ -66,14 +64,8 @@ class TinyColor {
return colorToHsv(_color);
}

HslColor toHsl() {
final hsl = rgbToHsl(
r: _color.red.toDouble(),
g: _color.green.toDouble(),
b: _color.blue.toDouble(),
);
return HslColor(
h: hsl.h * 360, s: hsl.s, l: hsl.l, a: _color.alpha.toDouble());
HSLColor toHsl() {
return HSLColor.fromColor(_color);
}

String toHex8() => _color.value.toRadixString(16).padLeft(8, '0');
Expand All @@ -84,9 +76,8 @@ class TinyColor {

TinyColor lighten([int amount = 10]) {
final hsl = this.toHsl();
hsl.l += amount / 100;
hsl.l = clamp01(hsl.l);
return TinyColor.fromHSL(hsl);
final lightness = clamp01(hsl.lightness + amount / 100);
return TinyColor.fromHSL(hsl.withLightness(lightness));
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

TinyColor brighten([int amount = 10]) {
Expand All @@ -102,9 +93,8 @@ class TinyColor {

TinyColor darken([int amount = 10]) {
final hsl = this.toHsl();
hsl.l -= amount / 100;
hsl.l = clamp01(hsl.l);
return TinyColor.fromHSL(hsl);
final lightness = clamp01(hsl.lightness - amount / 100);
return TinyColor.fromHSL(hsl.withLightness(lightness));
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

TinyColor tint([int amount = 10]) {
Expand All @@ -117,16 +107,14 @@ class TinyColor {

TinyColor desaturate([int amount = 10]) {
final hsl = this.toHsl();
hsl.s -= amount / 100;
hsl.s = clamp01(hsl.s);
return TinyColor.fromHSL(hsl);
final saturation = clamp01(hsl.saturation - amount / 100);
return TinyColor.fromHSL(hsl.withSaturation(saturation));
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

TinyColor saturate([int amount = 10]) {
final hsl = this.toHsl();
hsl.s += amount / 100;
hsl.s = clamp01(hsl.s);
return TinyColor.fromHSL(hsl);
final saturation = clamp01(hsl.saturation + amount / 100);
return TinyColor.fromHSL(hsl.withSaturation(saturation));
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

TinyColor greyscale() {
Expand All @@ -135,9 +123,8 @@ class TinyColor {

TinyColor spin(double amount) {
final hsl = this.toHsl();
final hue = (hsl.h + amount) % 360;
hsl.h = hue < 0 ? 360 + hue : hue;
return TinyColor.fromHSL(hsl);
final hue = (hsl.hue + amount) % 360;
return TinyColor.fromHSL(hsl.withHue(hue < 0 ? 360 + hue : hue);
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

TinyColor mix({required Color input, int amount = 50}) {
Expand All @@ -152,8 +139,8 @@ class TinyColor {

TinyColor complement() {
final hsl = this.toHsl();
hsl.h = (hsl.h + 180) % 360;
return TinyColor.fromHSL(hsl);
final hue = (hsl.hue + 180) % 360;
return TinyColor.fromHSL(hsl.withHue(hue));
calvintam236 marked this conversation as resolved.
Show resolved Hide resolved
}

Color get color {
Expand Down