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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [2.1.0] - TBA

### Changed
* `HslColor` class is now deprecated in favor of native `HSLColor` at [PR #18](https://github.com/TinyCommunity/tinycolor2/pull/35)
* Deprecated TinyColor.fromRGB() at [PR #40](https://github.com/TinyCommunity/tinycolor2/pull/40)

## [2.0.1] - 2022-03-11
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ The package uses [Pigment](https://pub.dartlang.org/packages/pigment) by [Bregy
TinyColor.fromString('#FE5567');
```

### From `HslColor`
### From `HSLColor`

```dart
final HslColor color = HslColor(h: 250, s: 57, l: 30);
final HSLColor color = HSLColor.fromAHSL(1.0, 250, 57, 30);
TinyColor.fromHSL(color);
```

Expand Down
3 changes: 1 addition & 2 deletions lib/src/color_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/painting.dart';

import 'hsl_color.dart';
import 'tinycolor.dart';

/// Extends the Color class to allow direct TinyColor manipulation natively
Expand All @@ -10,7 +9,7 @@ extension TinyColorExtension on Color {

HSVColor toHsv() => TinyColor(this).toHsv();

HslColor toHsl() => TinyColor(this).toHsl();
HSLColor toHsl() => TinyColor(this).toHsl();

/// Lighten the color a given amount, from 0 to 100. Providing 100 will always return white.
Color lighten([int amount = 10]) => TinyColor(this).lighten(amount).color;
Expand Down
17 changes: 7 additions & 10 deletions lib/src/conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import 'dart:math' as math;

import 'package:flutter/painting.dart';

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

HslColor rgbToHsl({
HSLColor rgbToHsl({
required double r,
required double g,
required double b,
Expand Down Expand Up @@ -35,18 +34,16 @@ HslColor rgbToHsl({
}

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

Color hslToColor(HslColor hsl) => 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 @@ -58,7 +55,7 @@ Color hslToRgb(HslColor hsl) {
b = _hue2rgb(p, q, h - 1 / 3);
}
return Color.fromARGB(
hsl.a.round(),
hsl.alpha.round(),
(r * 255).round(),
(g * 255).round(),
(b * 255).round(),
Expand Down
16 changes: 0 additions & 16 deletions lib/src/hsl_color.dart

This file was deleted.

48 changes: 15 additions & 33 deletions lib/src/tinycolor.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:math' as math;
import 'dart:ui' show Color;

import 'package:flutter/painting.dart' show HSVColor;
import 'package:flutter/painting.dart' show HSLColor, HSVColor;
import 'package:pigment/pigment.dart';

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

class TinyColor {
Expand All @@ -23,7 +22,7 @@ class TinyColor {
}) =>
TinyColor(Color.fromARGB(a, r, g, b));

factory TinyColor.fromHSL(HslColor hsl) => TinyColor(hslToColor(hsl));
factory TinyColor.fromHSL(HSLColor hsl) => TinyColor(hsl.toColor());

factory TinyColor.fromHSV(HSVColor hsv) => TinyColor(hsv.toColor());

Expand Down Expand Up @@ -51,29 +50,16 @@ class TinyColor {

HSVColor toHsv() => HSVColor.fromColor(_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() => HSLColor.fromColor(_color);

String toHex8() => _color.value.toRadixString(16).padLeft(8, '0');

TinyColor clone() => TinyColor(_color);

TinyColor lighten([int amount = 10]) {
final hsl = 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 Down Expand Up @@ -106,9 +92,8 @@ class TinyColor {

TinyColor darken([int amount = 10]) {
final hsl = 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]) => mix(
Expand All @@ -123,25 +108,22 @@ class TinyColor {

TinyColor desaturate([int amount = 10]) {
final hsl = 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 = 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() => desaturate(100);

TinyColor spin(double amount) {
final hsl = 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));
}

TinyColor mix(Color toColor, [int amount = 50]) {
Expand All @@ -157,8 +139,8 @@ class TinyColor {

TinyColor complement() {
final hsl = 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 => _color;
Expand Down
1 change: 0 additions & 1 deletion lib/tinycolor2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ library tinycolor2;

export 'src/color_extension.dart';
export 'src/conversion.dart';
export 'src/hsl_color.dart';
export 'src/tinycolor.dart';
export 'src/util.dart';
27 changes: 26 additions & 1 deletion test/tinycolor_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:ui' show Color;
import 'package:flutter/painting.dart' show Color, HSLColor, HSVColor;

import 'package:flutter_test/flutter_test.dart';
import 'package:tinycolor2/tinycolor2.dart';
Expand Down Expand Up @@ -285,4 +285,29 @@ void main() {
);
},
);

group(
"TinyColor HSL",
() {
final TinyColor color = TinyColor.fromHSL(HSLColor.fromColor(const Color(0xFFFFFFFF)));
test(
".color",
() {
expect(
color.color,
const Color(0xFFFFFFFF),
);
},
);
test(
".toHsl()",
() {
expect(
color.toHsl(),
HSLColor.fromColor(const Color(0xFFFFFFFF)),
);
},
);
},
);
}