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

Adds the new extension on Color for direct shortcut access #4

Merged
merged 8 commits into from
Jan 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [1.0.3]
* Added Color class extension method for direct usage
## [1.0.2]
*Fixed compatibility issues with dart 2.1 and pigment 1.0.3
* Fixed compatibility issues with dart 2.1 and pigment 1.0.3
## [1.0.1]
* Fixed HSLColor class name duplicate definition.
* Updated package description
Expand Down
67 changes: 61 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# tinycolor
> TinyColor is a small library for Flutter color manipulation and conversion
> TinyColor is a small library for Flutter color manipulation and conversion

A port of [tinycolor2](https://github.com/bgrins/TinyColor) by [Brian Grinstead](https://github.com/bgrins)

Expand All @@ -12,13 +12,14 @@ import 'package:tinycolor/tinycolor.dart';

final TinyColor = TinyColor(Colors.green);
```
Now you can also use the package to extend the native Color class with all the same features, but simpler. To use extension update, make sure to change envieronment sdk version in pubspec like this: ` sdk: ">=2.6.0 <3.0.0"`

### From a Hex String

The package uses [Pigment](https://pub.dartlang.org/packages/pigment) by [Bregy Malpartida Ramos](https://github.com/bregydoc/) to convert strings to `Color`

````dart
TinyColor.fromString('#FE5567';
TinyColor.fromString('#FE5567');
````

### From RGB int values
Expand All @@ -41,11 +42,17 @@ HSVColor color = HSVColor(h: 250, s: 57, v: 30);
TinyColor.fromHSV(color);
```

### From Flutter's Color

```dart
TinyColor tinyColor = Colors.blue.toTinyColor();
```

## Properties

### color

Returns de flutter `Color` after operations
Returns the flutter `Color` after operations

```dart
final Color color = TinyColor(Colors.white).color;
Expand All @@ -60,6 +67,8 @@ Returns the perceived brightness of a color, from `0-255`, as defined by [Web Co
```dart
TinyColor.fromString("#ffffff").getBrightness(); // 255
TinyColor.fromString("#000000").getBrightness(); // 0
// or with Color extension
Colors.grey.brightness; // 127
```

### isLight
Expand All @@ -69,6 +78,8 @@ Return a boolean indicating whether the color's perceived brightness is light.
```dart
TinyColor.fromString("#ffffff").isLight(); // true
TinyColor.fromString("#000000").isLight(); // false
// or with Color extension
Colors.white.isLight; // true
```

### isDark
Expand All @@ -78,6 +89,8 @@ Return a boolean indicating whether the color's perceived brightness is dark.
```dart
TinyColor.fromString("#ffffff").isDark(); // false
TinyColor.fromString("#000000").isDark(); // true
// or with Color extension
Colors.white.isDark; // false
```

### getLuminance
Expand All @@ -86,6 +99,8 @@ Return the perceived luminance of a color, a shorthand for flutter `Color.comput

```dart
TinyColor.fromString("#ffffff").getLuminance();
// or with Color extension
Colors.white.luminance;
```

### setAlpha
Expand All @@ -110,6 +125,8 @@ These methods manipulate the current color, and return it for chaining. For inst

```dart
TinyColor(Colors.red).lighten().desaturate().color;
// or with Color extension
Colors.red.lighten().desaturate();
```

### lighten
Expand All @@ -119,14 +136,18 @@ TinyColor(Colors.red).lighten().desaturate().color;
```dart
TinyColor(Colors.red).lighten().color;
TinyColor(Colors.red).lighten(100).color;
// or with Color extension
Colors.red.lighten(50);
```

### brighten

`brighten: function(amount = 10) -> TinyColor`. Brighten the color a given amount, from 0 to 100.

```dart
TinyColor(Colors.white).brighten().color;'
TinyColor(Colors.black).brighten().color;
// or with Color extension
Colors.black.brighten(50);
```

### darken
Expand All @@ -136,6 +157,8 @@ TinyColor(Colors.white).brighten().color;'
```dart
TinyColor(Colors.red).darken().color;
TinyColor(Colors.red).darken(100).color;
// or with Color extension
Colors.red.darken(50);
```

### tint
Expand All @@ -145,6 +168,8 @@ Mix the color with pure white, from 0 to 100. Providing 0 will do nothing, provi
```dart
TinyColor(Color.red).tint().color;
TinyColor(Color.red).tint(100).color;
// or with Color extension
Colors.red.tint(50);
```

### shade
Expand All @@ -154,6 +179,8 @@ Mix the color with pure black, from 0 to 100. Providing 0 will do nothing, provi
```dart
TinyColor(Colors.red).shade().color;
TinyColor(Colors.red).shade(100).color;
// or with Color extension
Colors.red.shade(50);
```

### desaturate
Expand All @@ -163,6 +190,8 @@ TinyColor(Colors.red).shade(100).color;
```dart
TinyColor(Colors.red).desaturate().color;
TinyColor(Colors.red).desaturate(100).color;
// or with Color extension
Colors.red.desaturate(50);
```

### saturate
Expand All @@ -171,14 +200,18 @@ TinyColor(Colors.red).desaturate(100).color;

```dart
TinyColor(Colors.red).saturate().color;
// or with Color extension
Colors.red.saturate(50);
```

### greyscale

`greyscale: function() -> TinyColor`. Completely desaturates a color into greyscale. Same as calling `desaturate(100)`.

```dart
TinyColor(Colors.red).greyscale().color;"
TinyColor(Colors.red).greyscale().color;
// or with Color extension
Colors.red.greyscale;
```

### spin
Expand All @@ -187,12 +220,34 @@ TinyColor(Colors.red).greyscale().color;"

```dart
TinyColor(Colors.red).spin(180).color;
// or with Color extension
Colors.red.spin(180);

// spin(0) and spin(360) do nothing
TinyColor(Colors.red).spin(0).color;
TinyColor(Colors.red).spin(360).color;
```

### compliment

`compliment: function() -> TinyColor`. Returns the Complimentary Color for dynamic matching.

```dart
TinyColor(Colors.red).compliment().color;
// or with Color extension
Colors.red.compliment;
```

### mix

`mix: function(toColor, amount = 10) -> TinyColor`. Blends the color with another color a given amount, from 0 - 100, default 50.

```dart
TinyColor(Colors.red).mix(TinyColor(Colors.yellow, 20)).color;
// or with Color extension
Colors.red.mix(Colors.yellow, 20);
```

## Common operations

### clone
Expand All @@ -204,4 +259,4 @@ Instantiate a new TinyColor object with the same color. Any changes to the new o
final color1 = new TinyColor(Colors.red);
final color2 = color1.clone();
color2.setAlpha(20);
```
```
57 changes: 57 additions & 0 deletions lib/color_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter/painting.dart';
import 'tinycolor.dart';

/// Extends the Color class to allow direct TinyColor manipulation nativly
extension TinyColorExtension on Color {
/// Converts standard Color to TinyColor object
TinyColor toTinyColor() => TinyColor(this);

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

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;

/// Brighten the color a given amount, from 0 to 100.
Color brighten([int amount = 10]) => TinyColor(this).brighten(amount).color;

/// Darken the color a given amount, from 0 to 100. Providing 100 will always return black.
Color darken([int amount = 10]) => TinyColor(this).darken(amount).color;

/// Mix the color with pure white, from 0 to 100. Providing 0 will do nothing, providing 100 will always return white.
Color tint([int amount = 10]) => TinyColor(this).tint(amount).color;

/// Mix the color with pure black, from 0 to 100. Providing 0 will do nothing, providing 100 will always return black.
Color shade([int amount = 10]) => TinyColor(this).shade(amount).color;

/// Desaturate the color a given amount, from 0 to 100. Providing 100 will is the same as calling greyscale.
Color desaturate([int amount = 10]) => TinyColor(this).desaturate(amount).color;

/// Saturate the color a given amount, from 0 to 100.
Color saturate([int amount = 10]) => TinyColor(this).saturate(amount).color;

/// Completely desaturates a color into greyscale. Same as calling desaturate(100).
Color get greyscale => TinyColor(this).greyscale().color;

/// Spin the hue a given amount, from -360 to 360. Calling with 0, 360, or -360 will do nothing (since it sets the hue back to what it was before).
Color spin([double amount = 0]) => TinyColor(this).spin(amount).color;

/// Returns the perceived brightness of a color, from 0-255, as defined by Web Content Accessibility Guidelines (Version 1.0).Returns the perceived brightness of a color, from 0-255, as defined by Web Content Accessibility Guidelines (Version 1.0).
double get brightness => TinyColor(this).getBrightness();

/// Return the perceived luminance of a color, a shorthand for flutter Color.computeLuminance
double get luminance => TinyColor(this).getLuminance();

/// Return a boolean indicating whether the color's perceived brightness is light.
bool get isLight => TinyColor(this).isLight();

/// Return a boolean indicating whether the color's perceived brightness is dark.
bool get isDark => TinyColor(this).isDark();

/// Returns the Complimentary Color for dynamic matching
Color get compliment => TinyColor(this).complement().color;

/// Blends the color with another color a given amount, from 0 - 100, default 50.
Color mix(Color toColor, [int amount = 50]) => TinyColor(this).mix(input: toColor, amount: amount).color;
}
1 change: 1 addition & 0 deletions lib/tinycolor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'hsl_color.dart';
import 'util.dart';

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

class TinyColor {
Color originalColor;
Expand Down
39 changes: 2 additions & 37 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: tinycolor
description: Flutter Color manipulation and conversion, ported from JS tinycolor2
version: 1.0.2
version: 1.0.3
author: Foo() <[email protected]>
homepage: https://github.com/FooStudio/tinycolor

environment:
sdk: ">=2.0.0-dev.48.0 <3.0.0"
sdk: ">=2.6.0 <3.0.0"
flutter: ">=0.1.4 <3.0.0"

dependencies:
Expand All @@ -17,39 +17,4 @@ dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.io/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.

# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.io/custom-fonts/#from-packages
3 changes: 2 additions & 1 deletion test/util_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
//import 'package:test/test.dart';
import 'package:tinycolor/util.dart';

void main() {
Expand Down