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

feat: Extended color faker with rgb method #72

Merged
merged 2 commits into from
Apr 17, 2024
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 2.2.0

### Features

- Add `rgbColor` method to colors faker.
```dart
faker.colors.rgbColor();
faker.colors.rgbColor(prefix: '0x');
faker.colors.rgbColor(casing: Casing.upper);
faker.colors.rgbColor(format: Format.css);
faker.colors.rgbColor(includeAlpha: true);
```

### Fixes
- Fix safe email method by removing apostrophe. [PR #70](https://github.com/drager/faker/pull/70)

### Maintenance
- Update lints package. [PR #71](https://github.com/drager/faker/pull/71)

## 2.1.0

### Features
Expand Down
1 change: 1 addition & 0 deletions lib/faker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export 'src/faker.dart';
export 'src/random_generator.dart';

export 'src/address.dart';
export 'src/colors.dart';
export 'src/conference.dart';
export 'src/company.dart';
export 'src/currency.dart';
Expand Down
77 changes: 77 additions & 0 deletions lib/src/colors.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import 'data/colors/colors.dart';
import 'random_generator.dart';

enum Casing {
lower,
upper,
}

enum Format {
hex,
css,
}

class Color {
static const _hexChars = '0123456789abcdef';

const Color(this.random);

final RandomGenerator random;
Expand All @@ -21,4 +33,69 @@ class Color {
/// faker.colors.commonColor();
/// ```
String commonColor() => random.element(commonColors);

/// Generates a [String] rgb color.
///
/// Example
/// ```dart
/// faker.colors.rgbColor();
/// faker.colors.rgbColor(prefix: '0x');
/// faker.colors.rgbColor(casing: Casing.upper);
/// faker.colors.rgbColor(casing: Casing.lower);
/// faker.colors.rgbColor(prefix: '#', casing: Casing.lower);
/// faker.colors.rgbColor(format: Format.hex, casing: Casing.lower');
/// faker.colors.rgbColor(format: Format.css);
/// faker.colors.rgbColor(includeAlpha: true);
/// faker.colors.rgbColor(format: Format.css, includeAlpha: true);
/// ```
String rgbColor({
final String prefix = '#',
final Casing casing = Casing.lower,
final Format format = Format.hex,
final bool includeAlpha = false,
}) {
if (format == Format.hex) {
var length = includeAlpha ? 8 : 6;
String color = '';
while (length-- > 0) {
color += _hexChars[(random.integer(16)) | 0];
}

return _formatHexColor(color, prefix: prefix, casing: casing);
}

var color = List.generate(3, (_) => random.integer(255));
if (includeAlpha) {
return 'rgba(${color[0]}, ${color[1]}, ${color[2]}, ${random.decimal()})';
}

return 'rgb(${color[0]}, ${color[1]}, ${color[2]})';
}

String _formatHexColor(
String hexColor, {
required Casing casing,
String? prefix,
}) {
switch (casing) {
case Casing.upper:
{
hexColor = hexColor.toUpperCase();
break;
}
case Casing.lower:
{
hexColor = hexColor.toLowerCase();
break;
}
default:
break;
}

if (prefix != null) {
hexColor = prefix + hexColor;
}

return hexColor;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: faker
version: 2.1.1
version: 2.2.0
description: A library for generating fake data. faker is heavily inspired by the Python package faker and, the Ruby package ffaker.
homepage: https://github.com/drager/faker
environment:
Expand Down
45 changes: 45 additions & 0 deletions test/specs/colors.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:faker/src/colors.dart';
import 'package:test/test.dart';
import 'package:faker/faker.dart';

Expand All @@ -20,5 +21,49 @@ void main() {
matches(r"^[\w,' ]+[\w ]$")));
}
});

test('should be able to generate a rgb color', () {
final color = faker.color.rgbColor();
expect(color, matches(r'^(#[a-f0-9]{6})$'));
});

test('should be able to generate a rgb color with 0x prefix', () {
final color = faker.color.rgbColor(prefix: '0x');
expect(color, matches(r'^(0x[a-f0-9]{6})$'));
});

test('should be able to generate a rgb color with 0x prefix lowercase', () {
final color = faker.color.rgbColor(prefix: '0x', casing: Casing.lower);
expect(color, matches(r'^(0x[a-f0-9]{6})$'));
});

test('should be able to generate a rgb color with 0x prefix uppercase', () {
final color = faker.color.rgbColor(prefix: '0x', casing: Casing.upper);
expect(color, matches(r'^(0x[A-F0-9]{6})$'));
});

test('should be able to generate a rgb color using css format', () {
final color = faker.color.rgbColor(format: Format.css);
expect(color, matches(r'^(rgb\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}\))$'));
});

test('should be able to generate a rgb color using alpha', () {
final color = faker.color.rgbColor(includeAlpha: true);
expect(color, matches(r'^(#[a-fA-F0-9]{8})$'));
});

test(
'should be able to generate a rgb color using css format and alpha',
() {
final color = faker.color.rgbColor(
format: Format.css,
includeAlpha: true,
);
expect(
color,
matches(r'^(rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, \d*\.?\d*\))$'),
);
},
);
});
}