Skip to content

Commit

Permalink
feat: create GradientSwatch and GradOf
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamizes committed Oct 20, 2021
1 parent e822170 commit b51aee2
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 45 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Add

- `GradOf` class with the constructors `blueGrey`, `grey`, and `brown`.
- `GradientSwatch` helper abstract class.

### Changed

- stricter lint rules.
- improvements in README.
- Rename 'o' parameter to 'opacity' in `white` and `black` constructors of the
`PaletteRGB` class — **BREAKING CHANGES**.
- improvements to README file.
- rename 'o' parameter to 'opacity' in `white` and `black` constructors of the
`PaletteRGB` class — **BREAKING CHANGE**.
- rename `GradientImmu` class to `GradientImmut`**BREAKING CHANGE**.

### Removed

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ region. The colors produced by a gradient vary continuously with position,
producing smooth color transitions.

While the `Swatch` interface retrieves an `iterable<Colors>` object, subclasses
of `Gradients` retrieves a `List<Colors>`, which makes them better suited for
of `Gradient` retrieves a `List<Colors>`, which makes them better suited for
dealing with Flutter's gradient APIs — these APIs almost always expects a
`List<Color>` object as a parameter instead of an `Iterable<Color>` object.

An example of a `Gradient` implementation is the abstract class `GradientImmu`,
which retrieves immutable `List<Colors>` objects.
For Material Design color gradients, use the `GradOf` class, which in turn
implements the `Gradient` interface. Another example of a `Gradient`
implementation is the abstract class `GradientImmu`, which retrieves immutable
`List<Colors>` objects.

For a complete list of gradients:

Expand Down
6 changes: 3 additions & 3 deletions lib/gradients.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Gradients
///
/// Suitable classes for working with Flutter's Gradient Apis.
library gradients;

export 'src/gradient.dart';
export 'src/gradient_immu.dart';
export 'src/gradient_immut.dart';
export 'src/gradient_swatch.dart';
export 'src/material/grad_of.dart';
2 changes: 2 additions & 0 deletions lib/src/gradient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'swatch.dart';
/// See also:
/// - [color-gradient](https://en.wikipedia.org/wiki/Color_gradient)
abstract class Gradient implements Swatch {
const Gradient();

/// A list of position-dependent colors — a color gradient.
@override
List<Color> get colors;
Expand Down
18 changes: 0 additions & 18 deletions lib/src/gradient_immu.dart

This file was deleted.

16 changes: 16 additions & 0 deletions lib/src/gradient_immut.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:ui';

import 'package:eo_color/src/palette.dart';

import 'gradient.dart';
import 'swatch_base.dart';

/// Convenience [Gradient] that retrieves an immutable list of colors.
abstract class GradientImmut extends SwatchBase implements Gradient {
/// [palettes] is a source of colors to be transformed into an immutable list.
const GradientImmut(Iterable<Palette> palettes) : super(palettes);

/// An **immutable list** of position-dependent colors — a color gradient.
@override
List<Color> get colors => List.unmodifiable(super.colors);
}
20 changes: 20 additions & 0 deletions lib/src/gradient_swatch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:ui';

import 'gradient.dart';
import 'swatch.dart';

/// A [Gradient] from a [Swatch] instance.
abstract class GradientSwatch implements Gradient {
/// A color gradient from a color [Swatch].
///
/// [growable] wheather or not to create a list that can grow (expand).
const GradientSwatch(Swatch swatch, {bool growable = false})
: _swatch = swatch,
_growable = growable;

final Swatch _swatch;
final bool _growable;

@override
List<Color> get colors => List.of(_swatch.colors, growable: _growable);
}
12 changes: 6 additions & 6 deletions lib/src/material/black/black.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class Black extends PaletteRGB {
const Black() : super.black();

/// 87% opacity.
const Black.opacity87() : super.black(opacity:0xDD);
const Black.opacity87() : super.black(opacity: 0xDD);

/// 54% opacity.
const Black.opacity54() : super.black(opacity:0x8A);
const Black.opacity54() : super.black(opacity: 0x8A);

/// 45% opacity.
const Black.opacity45() : super.black(opacity:0x73);
const Black.opacity45() : super.black(opacity: 0x73);

/// 38% opacity.
const Black.opacity38() : super.black(opacity:0x61);
const Black.opacity38() : super.black(opacity: 0x61);

/// 26% opacity.
const Black.opacity26() : super.black(opacity:0x42);
const Black.opacity26() : super.black(opacity: 0x42);

/// 12% opacity.
const Black.opacity12() : super.black(opacity:0x1F);
const Black.opacity12() : super.black(opacity: 0x1F);
}
23 changes: 23 additions & 0 deletions lib/src/material/blue_grey/blue_grey_grad.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:ui';

import '../../gradient.dart';
import 'blue_greys.dart';

/// A gradient of ten shades of blue-grey.
///
/// See also: [BlueGreys].
class BlueGreyGrad extends Gradient {
/// Ten shades of blue-grey; the higher the index, the darker the color.
///
/// Creates a growable list when [growable] is true; otherwise, it returns a
/// fixed-length list.
///
/// See also: [BlueGreys].
const BlueGreyGrad({bool growable = false}) : _growable = growable;

static const _src = BlueGreys();
final bool _growable;

@override
List<Color> get colors => List.of(_src.colors, growable: _growable);
}
26 changes: 26 additions & 0 deletions lib/src/material/grad_of.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../../swatches.dart';
import '../gradient_swatch.dart';

/// The gradient of the shades of a Material Design color.
///
/// It produces a list of colors of fixed or growable length, ordered by the
/// darkness of the colors: the higher the index, the darker the color.
class GradOf extends GradientSwatch {
/// A gradient of ten shades of blue-grey.
///
/// See also: [BlueGreys].
const GradOf.blueGrey({bool growable = false})
: super(const BlueGreys(), growable: growable);

/// A gradient of ten shades of brown.
///
/// See also: [Browns].
const GradOf.brown({bool growable = false})
: super(const Browns(), growable: growable);

/// A gradient of ten shades of grey.
///
/// See also: [Greys].
const GradOf.grey({bool growable = false})
: super(const Greys(), growable: growable);
}
16 changes: 8 additions & 8 deletions lib/src/material/white/white.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ class White extends PaletteRGB {
const White() : super.white();

/// 70% opacity.
const White.opacity70() : super.white(opacity:0xB3);
const White.opacity70() : super.white(opacity: 0xB3);

/// 60% opacity.
const White.opacity60() : super.white(opacity:0x99);
const White.opacity60() : super.white(opacity: 0x99);

/// 54% opacity.
const White.opacity54() : super.white(opacity:0x8A);
const White.opacity54() : super.white(opacity: 0x8A);

/// 38% opacity.
const White.opacity38() : super.white(opacity:0x62);
const White.opacity38() : super.white(opacity: 0x62);

/// 30% opacity.
const White.opacity30() : super.white(opacity:0x4D);
const White.opacity30() : super.white(opacity: 0x4D);

/// 24% opacity.
const White.opacity24() : super.white(opacity:0x3D);
const White.opacity24() : super.white(opacity: 0x3D);

/// 12% opacity.
const White.opacity12() : super.white(opacity:0x1F);
const White.opacity12() : super.white(opacity: 0x1F);

/// 10% opacity.
const White.opacity10() : super.white(opacity:0x1A);
const White.opacity10() : super.white(opacity: 0x1A);
}
6 changes: 4 additions & 2 deletions lib/src/palette_rgb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ abstract class PaletteRGB implements Palette {
/// Convenience white #FFFFFF.
///
/// [opacity] the opacity value; it defaults to 0xFF - fully opaque.
const PaletteRGB.white({int opacity = 0xFF}) : this(alpha: opacity, rgb: 0xFFFFFF);
const PaletteRGB.white({int opacity = 0xFF})
: this(alpha: opacity, rgb: 0xFFFFFF);

/// Convenience black #000000.
///
/// [opacity] the opacity value; it defaults to 0xFF - fully opaque.
const PaletteRGB.black({int opacity = 0xFF}) : this(alpha: opacity, rgb: 0x000000);
const PaletteRGB.black({int opacity = 0xFF})
: this(alpha: opacity, rgb: 0x000000);

/// the color's 32 bits.
final int _value;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/swatch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'dart:ui';
/// A group of related colors such as the colors of the rainbow, shades of grey,
/// etc.
abstract class Swatch {
/// const ctor.
const Swatch();

/// The colors.
///
/// The order of the colors varies between swatches. Some swatches might order
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:eo_color/eo_color.dart';
import 'package:eo_color/src/gradient_immut.dart';
import 'package:eo_color/src/material/grey/grey.dart';
import 'package:eo_color/src/material/transparent.dart';
import 'package:flutter_test/flutter_test.dart';

const aGrey = [Grey()];
Expand All @@ -10,7 +12,7 @@ const manyGreys = [
Grey.dark(),
];

class GradTest extends GradientImmu {
class GradTest extends GradientImmut {
const GradTest.empty() : super(const []);
const GradTest.single() : super(aGrey);
const GradTest.many() : super(manyGreys);
Expand Down
58 changes: 58 additions & 0 deletions test/material/grad_of_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:eo_color/src/material/blue_grey/blue_greys.dart';
import 'package:eo_color/src/material/brown/browns.dart';
import 'package:eo_color/src/material/grad_of.dart';
import 'package:eo_color/src/material/grey/greys.dart';
import 'package:eo_color/src/material/transparent.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('GradOf', () {
final aColor = const Transparent().color;
group('blueGrey:', () {
final blueGreyGrad = const GradOf.blueGrey().colors;
test('gradient', () {
expect(blueGreyGrad, List.of(const BlueGreys().colors));
});
test('fixed-length list', () {
/// Should not support adding colors to the list.
expect(() => blueGreyGrad.add(aColor), throwsUnsupportedError);
});
test('growable list', () {
final growable = const GradOf.blueGrey(growable: true).colors;
final prevLength = growable.length;
growable.add(aColor);
expect(growable.length, prevLength + 1);
});
});
group('grey:', () {
final greyGrad = const GradOf.grey().colors;
test('gradient', () {
expect(greyGrad, List.of(const Greys().colors));
});
test('fixed-length list', () {
expect(() => greyGrad.add(aColor), throwsUnsupportedError);
});
test('growable list', () {
final growable = const GradOf.grey(growable: true).colors;
final prevLength = growable.length;
growable.add(aColor);
expect(growable.length, prevLength + 1);
});
});
group('brown:', () {
final brownGrad = const GradOf.brown().colors;
test('gradient', () {
expect(brownGrad, List.of(const Browns().colors));
});
test('fixed-length list', () {
expect(() => brownGrad.add(aColor), throwsUnsupportedError);
});
test('growable list', () {
final growable = const GradOf.brown(growable: true).colors;
final prevLength = growable.length;
growable.add(aColor);
expect(growable.length, prevLength + 1);
});
});
});
}

0 comments on commit b51aee2

Please sign in to comment.