Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Martens committed Jul 29, 2022
1 parent 4eacf3f commit 6ce03df
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
1 change: 0 additions & 1 deletion lib/src/options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter_randomcolor/flutter_randomcolor.dart';
import 'package:flutter_randomcolor/src/colortype.dart';

class Options {
final dynamic colorType;
Expand Down
27 changes: 10 additions & 17 deletions lib/src/randomcolor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ library flutter_randomcolor;
import 'dart:math' as math;

import 'package:flutter_randomcolor/flutter_randomcolor.dart';
import 'package:flutter_randomcolor/src/colortype.dart';
import 'package:flutter_randomcolor/src/range.dart';

class RandomColor {
// color dictionary that save all the colors
static Map<ColorType?, DefinedColor> _colorDictionary =
Map<ColorType?, DefinedColor>();
// ignore: unused_field
static math.Random _random = math.Random(); //seed data

// get random colors based on options provided
static getColor(Options options) {
__loadColorBounds();
// check if count is not provided or less than 2 then return a single color
if (options.count == null || options.count < 2) return _pick(options);
if (options.count < 2) return _pick(options);
var colors = [];
// if coloor count is more than 1 return an array of color
// if color count is more than 1 return an array of color
for (var i = 0; i < options.count; i++) {
var _color = _pick(options);
colors.add(_color);
Expand All @@ -39,22 +39,21 @@ class RandomColor {

// generate a random value
static void seed(int seed) {
if (seed == null) {
_random = math.Random();
return;
}
_random = math.Random(seed);
}

// define the color based on provided hue and lowerBounds
static _defineColor(
{ColorType? colorType, List<int>? hueRange, required List<List> lowerBounds}) {
{ColorType? colorType,
List<int>? hueRange,
required List<List> lowerBounds}) {
var sMin = lowerBounds[0][0]; // saturation lowerbound
var sMax = lowerBounds[lowerBounds.length - 1][0]; // saturation upperbound
var bMin = lowerBounds[lowerBounds.length - 1][1]; // brightness lowerbound
var bMax = lowerBounds[0][1]; // brightness upperbound

_colorDictionary[colorType] = DefinedColor(
//only assign is hueRange is not null
hueRange: hueRange != null ? Range.toRange(hueRange) : null,
lowerBounds: lowerBounds.map((e) => Point(x: e[0], y: e[1])).toList(),
brightnessRange: Range(upper: bMax, lower: bMin),
Expand Down Expand Up @@ -172,35 +171,28 @@ class RandomColor {
switch (options.format) {
case Format.hsvArray:
return [hue, saturation, value];
break;
case Format.hslArray:
return _hsvToHsl(hue, saturation, value);
break;
case Format.hsl:
var hsl = _hsvToHsl(hue, saturation, value);
return 'hsl(${hsl[0]}, ${hsl[1].toInt()}%, ${hsl[2].toInt()}%)';
break;
case Format.hsla:
var _hsl = _hsvToHsl(hue, saturation, value);
var alpha =
options.alpha ?? math.Random().nextDouble().toStringAsFixed(1);
return 'hsla(${_hsl[0]}, ${_hsl[1]}%, ${_hsl[2]}%,$alpha)';
break;
case Format.rgbArray:
return _hsvToRGB(hue: hue, saturation: saturation, value: value);
break;
case Format.rgb:
List<int> rgb =
_hsvToRGB(hue: hue, saturation: saturation, value: value);
return 'rgb(${rgb.join(',')})';
break;
case Format.rgba:
List<int> rgb =
_hsvToRGB(hue: hue, saturation: saturation, value: value);
var alpha =
options.alpha ?? math.Random().nextDouble().toStringAsFixed(1);
return 'rgba(${rgb.join(',')},$alpha)';
break;
case Format.hex:
return _hsvToHex(hue, saturation, value);
default:
Expand Down Expand Up @@ -242,7 +234,6 @@ class RandomColor {
hue <= element.value.hueRange!.upper!)
.first;

assert(result.value != null);
return result.value;
}

Expand Down Expand Up @@ -290,7 +281,8 @@ class RandomColor {
}

//convert hsv to RGB color format
static List<int> _hsvToRGB({required hue, required saturation, required value}) {
static List<int> _hsvToRGB(
{required hue, required saturation, required value}) {
var h = hue.toDouble();
if (h == 0) {
h = 1;
Expand Down Expand Up @@ -481,6 +473,7 @@ class RandomColor {
);
}
}

class DefinedColor {
final Range? hueRange;
final Range? saturationRange;
Expand Down
5 changes: 1 addition & 4 deletions lib/src/range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ class Range {
switch (index) {
case 0:
return lower;
break;
case 1:
return upper;
break;
default:
}
return 0;
Expand All @@ -28,8 +26,7 @@ class Range {
}
}

factory Range.toRange(List<int>? range) {
if (range == null) return null!;
factory Range.toRange(List<int> range) {
return Range(
lower: range[0],
upper: range[1],
Expand Down
39 changes: 19 additions & 20 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -42,14 +42,14 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -66,21 +66,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -92,7 +99,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -127,21 +134,13 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.9"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.17.0"
dart: ">=2.17.0-0 <3.0.0"

0 comments on commit 6ce03df

Please sign in to comment.