diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d5903c..8c4f117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Gradient interface — [83](https://github.com/dartoos-dev/eo_color/issues/83). - doc comments about the purpose of each library. +### Changed + +- add a code quality badge to REAME. + ## [1.0.0] - 2021-06-19 ### Changed diff --git a/lib/eo_color.dart b/lib/eo_color.dart index e7d11cb..183538b 100644 --- a/lib/eo_color.dart +++ b/lib/eo_color.dart @@ -5,5 +5,6 @@ /// - an elegant, object-oriented framework for more specific color packages. library eo_color; +export 'gradients.dart'; export 'palettes.dart'; export 'swatches.dart'; diff --git a/lib/gradients.dart b/lib/gradients.dart new file mode 100644 index 0000000..0f32185 --- /dev/null +++ b/lib/gradients.dart @@ -0,0 +1,7 @@ +/// Gradients +/// +/// Suitable classes for working with Flutter's Gradient Apis. +library gradients; + +export 'src/gradient.dart'; +export 'src/gradient_immu.dart'; diff --git a/lib/palettes.dart b/lib/palettes.dart index cb9354b..7293a2e 100644 --- a/lib/palettes.dart +++ b/lib/palettes.dart @@ -1,7 +1,7 @@ /// Palettes. /// /// `Palette` is a general purpose interface that represents any color palette -/// from which a single color can be picked at a time. +/// from which a single color can be selected at a time. library palettes; export 'src/material/amber/amber.dart'; diff --git a/lib/src/gradient.dart b/lib/src/gradient.dart new file mode 100644 index 0000000..e2f8a7c --- /dev/null +++ b/lib/src/gradient.dart @@ -0,0 +1,18 @@ +import 'dart:ui'; + +import 'swatch.dart'; + +/// Represents a color gradient. +/// +/// A Color Gradient specifies a range of position-dependent colors, usually +/// used to fill a region. For example, many window managers allow the screen +/// background to be specified as a gradient. The colors produced by a gradient +/// vary continuously with position, producing smooth color transitions. +/// +/// See also: +/// - [color-gradient](https://en.wikipedia.org/wiki/Color_gradient) +abstract class Gradient implements Swatch { + /// A list of position-dependent colors — a color gradient. + @override + List get colors; +} diff --git a/lib/src/gradient_immu.dart b/lib/src/gradient_immu.dart new file mode 100644 index 0000000..41fbf86 --- /dev/null +++ b/lib/src/gradient_immu.dart @@ -0,0 +1,22 @@ +import 'dart:ui'; + +import 'package:eo_color/src/palette.dart'; + +import 'gradient.dart'; +import 'swatch_base.dart'; + +/// Convenience [Gradient]. +/// +/// It retrieves an immutable list of colors. +abstract class GradientImmu extends SwatchBase implements Gradient { + /// [palettes] is the iterable of colors to be turned into an immutable list. + const GradientImmu(Iterable palettes) : super(palettes); + + /// An **immutable list** of position-dependent colors — a color gradient. + @override + List get colors => List.unmodifiable(super.colors); + + /// Forwards to [colors]. + @override + List call() => colors; +} diff --git a/lib/src/palette_rgb.dart b/lib/src/palette_rgb.dart index 3335467..67ee09b 100644 --- a/lib/src/palette_rgb.dart +++ b/lib/src/palette_rgb.dart @@ -31,6 +31,7 @@ abstract class PaletteRGB implements Palette { /// the color's 32 bits. final int _value; + /// The rgb color. @override Color get color => Color(_value); diff --git a/lib/src/swatch_base.dart b/lib/src/swatch_base.dart index 672db8b..b0bb968 100644 --- a/lib/src/swatch_base.dart +++ b/lib/src/swatch_base.dart @@ -13,6 +13,7 @@ abstract class SwatchBase implements Swatch { // Source of picked colors. final Iterable _palettes; + /// A new lazy [Iterable] of [Color]. @override Iterable get colors => _palettes.map((palette) => palette.color); diff --git a/test/gradient_immu_test.dart b/test/gradient_immu_test.dart new file mode 100644 index 0000000..4dd7313 --- /dev/null +++ b/test/gradient_immu_test.dart @@ -0,0 +1,53 @@ +import 'package:eo_color/eo_color.dart'; +import 'package:flutter_test/flutter_test.dart'; + +const aGrey = [Grey()]; +const manyGreys = [ + Grey.light(), + Grey.bitLighter(), + Grey(), + Grey.bitDarker(), + Grey.dark(), +]; + +class GradTest extends GradientImmu { + const GradTest.empty() : super(const []); + const GradTest.single() : super(aGrey); + const GradTest.many() : super(manyGreys); +} + +void main() { + group('GradientImmu class', () { + test('empty', () { + const emptyList = GradTest.empty(); + expect(emptyList.colors.isEmpty, true); + }); + test('single', () { + const singleElem = GradTest.single(); + expect(singleElem.colors.length, 1); + expect(singleElem().first, aGrey.first.color); + }); + test('many', () { + const manyElems = GradTest.many(); + expect(manyElems.colors.length, manyGreys.length); + expect(manyElems.colors, manyGreys.map((p) => p.color).toList()); + }); + test('immutability', () { + final emptyImmuList = const GradTest.empty().colors; + expect( + () => emptyImmuList.add(const Transparent().color), + throwsUnsupportedError, + ); + final singleElemImmuList = const GradTest.single().colors; + expect( + () => singleElemImmuList.add(const Transparent().color), + throwsUnsupportedError, + ); + final manyElemsImmuList = const GradTest.many().colors; + expect( + () => manyElemsImmuList.add(const Transparent().color), + throwsUnsupportedError, + ); + }); + }); +}