Skip to content

Commit

Permalink
feat: create Gradient and GradientImmu
Browse files Browse the repository at this point in the history
Closes #83
  • Loading branch information
rafamizes committed Jun 26, 2021
1 parent d126683 commit 43d54b3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/eo_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
7 changes: 7 additions & 0 deletions lib/gradients.dart
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion lib/palettes.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
18 changes: 18 additions & 0 deletions lib/src/gradient.dart
Original file line number Diff line number Diff line change
@@ -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<Color> get colors;
}
22 changes: 22 additions & 0 deletions lib/src/gradient_immu.dart
Original file line number Diff line number Diff line change
@@ -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<Palette> palettes) : super(palettes);

/// An **immutable list** of position-dependent colors — a color gradient.
@override
List<Color> get colors => List.unmodifiable(super.colors);

/// Forwards to [colors].
@override
List<Color> call() => colors;
}
1 change: 1 addition & 0 deletions lib/src/palette_rgb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions lib/src/swatch_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class SwatchBase implements Swatch {
// Source of picked colors.
final Iterable<Palette> _palettes;

/// A new lazy [Iterable] of [Color].
@override
Iterable<Color> get colors => _palettes.map((palette) => palette.color);

Expand Down
53 changes: 53 additions & 0 deletions test/gradient_immu_test.dart
Original file line number Diff line number Diff line change
@@ -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,
);
});
});
}

0 comments on commit 43d54b3

Please sign in to comment.