Add random to the Color and Paint extensions (#1081)

* Add `randomColor` to the `Color` extension

* Add flame rive package to monorepo (#1048)

Add flame rive package to monorepo

* Add random to PaintExtension

* Update examples/pubspec.yaml

Co-authored-by: Erick <erickzanardoo@gmail.com>

Co-authored-by: Renan <6718144+renancaraujo@users.noreply.github.com>
Co-authored-by: Erick <erickzanardoo@gmail.com>
This commit is contained in:
Lukas Klingsbo
2021-11-10 13:09:12 +01:00
committed by GitHub
parent 92f3346966
commit 32bf19b91c
9 changed files with 115 additions and 56 deletions

View File

@ -1,11 +1,9 @@
import 'dart:math';
import 'dart:ui'; import 'dart:ui';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame/palette.dart';
const priorityInfo = ''' const priorityInfo = '''
On this example, click on the square to bring them to the front by changing the On this example, click on the square to bring them to the front by changing the
@ -18,7 +16,7 @@ class Square extends PositionComponent with HasGameRef<Priority>, Tappable {
Square(Vector2 position) { Square(Vector2 position) {
this.position.setFrom(position); this.position.setFrom(position);
size.setValues(100, 100); size.setValues(100, 100);
paint = _randomPaint(); paint = PaintExtension.random(withAlpha: 0.9, base: 100);
} }
@override @override
@ -35,17 +33,6 @@ class Square extends PositionComponent with HasGameRef<Priority>, Tappable {
super.render(canvas); super.render(canvas);
canvas.drawRect(size.toRect(), paint); canvas.drawRect(size.toRect(), paint);
} }
static Paint _randomPaint() {
final rng = Random();
final color = Color.fromRGBO(
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256),
0.9,
);
return PaletteEntry(color).paint();
}
} }
class Priority extends FlameGame with HasTappableComponents { class Priority extends FlameGame with HasTappableComponents {

View File

@ -1,26 +1,13 @@
import 'dart:math' as math;
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class TappableSquare extends PositionComponent with Tappable { class TappableSquare extends PositionComponent with Tappable {
static Paint _randomPaint() {
final rng = math.Random();
final color = Color.fromRGBO(
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256),
0.9,
);
return PaletteEntry(color).paint();
}
Paint currentPaint; Paint currentPaint;
TappableSquare({Vector2? position}) TappableSquare({Vector2? position})
: currentPaint = _randomPaint(), : currentPaint = PaintExtension.random(withAlpha: 0.9, base: 100),
super( super(
position: position ?? Vector2.all(100), position: position ?? Vector2.all(100),
size: Vector2.all(100), size: Vector2.all(100),

View File

@ -17,6 +17,7 @@
- Create default implementations of `RectangleComponent`, `CircleComponent` and `PolygonComponent` - Create default implementations of `RectangleComponent`, `CircleComponent` and `PolygonComponent`
- Streamlined the argument list for all components extending `PositionComponent` - Streamlined the argument list for all components extending `PositionComponent`
- Improved interaction between viewport and isHud components - Improved interaction between viewport and isHud components
- `randomColor` method in the `Color` extension
- Calling super-method in `.render()` is now optional - Calling super-method in `.render()` is now optional
- Components that manipulate canvas state are now responsible for saving/restoring that state - Components that manipulate canvas state are now responsible for saving/restoring that state

View File

@ -3,6 +3,7 @@ export 'src/extensions/color.dart';
export 'src/extensions/image.dart'; export 'src/extensions/image.dart';
export 'src/extensions/matrix4.dart'; export 'src/extensions/matrix4.dart';
export 'src/extensions/offset.dart'; export 'src/extensions/offset.dart';
export 'src/extensions/paint.dart';
export 'src/extensions/rect.dart'; export 'src/extensions/rect.dart';
export 'src/extensions/rectangle.dart'; export 'src/extensions/rectangle.dart';
export 'src/extensions/size.dart'; export 'src/extensions/size.dart';

View File

@ -1 +1,3 @@
export 'src/extensions/color.dart';
export 'src/extensions/paint.dart';
export 'src/palette.dart'; export 'src/palette.dart';

View File

@ -1,3 +1,4 @@
import 'dart:math';
import 'dart:ui'; import 'dart:ui';
export 'dart:ui' show Color; export 'dart:ui' show Color;
@ -101,4 +102,27 @@ extension ColorExtension on Color {
components[3], components[3],
); );
} }
/// Generates a random [Color] with the set [withAlpha] or the default (1.0).
/// You can pass in a random number generator [rng], if omitted the function
/// will create a new [Random] object without a seed and use that.
/// [base] can be used to get the random colors in only a lighter spectrum, it
/// should be between [0-256].
static Color random({
double withAlpha = 1.0,
int base = 0,
Random? rng,
}) {
assert(
base >= 0 && base <= 256,
'The base argument should be between 0-256',
);
rng ??= Random();
return Color.fromRGBO(
base + (base == 256 ? 0 : rng.nextInt(256 - base)),
base + (base == 256 ? 0 : rng.nextInt(256 - base)),
base + (base == 256 ? 0 : rng.nextInt(256 - base)),
withAlpha,
);
}
} }

View File

@ -0,0 +1,81 @@
import 'dart:math';
import 'dart:ui';
import 'color.dart';
export 'dart:ui' show Color;
extension PaintExtension on Paint {
/// Darken the shade of the [Color] in the [Paint] object by the [amount].
///
/// [amount] is a double between 0 and 1.
///
/// Based on: https://stackoverflow.com/a/60191441.
void darken(double amount) {
color = color.darken(amount);
}
/// Brighten the shade of the [Color] in the [Paint] object by the [amount].
///
/// [amount] is a double between 0 and 1.
///
/// Based on: https://stackoverflow.com/a/60191441.
void brighten(double amount) {
color = color.brighten(amount);
}
/// Parses an RGB color from a valid hex string (e.g. #1C1C1C).
///
/// The `#` is optional.
/// The short-hand syntax is support, e.g.: #CCC.
/// Lower-case letters are supported.
///
/// Examples of valid inputs:
/// ccc, CCC, #ccc, #CCC, #c1c1c1, #C1C1C1, c1c1c1, C1C1C1
///
/// If the string is not valid, an error is thrown.
///
/// Note: if you are hardcoding colors, use Dart's built-in hexadecimal
/// literals instead.
static Paint fromRGBHexString(String hexString) {
final color = ColorExtension.fromRGBHexString(hexString);
return Paint()..color = color;
}
/// Parses an ARGB color from a valid hex string (e.g. #1C1C1C).
///
/// The `#` is optional.
/// The short-hand syntax is support, e.g.: #CCCC.
/// Lower-case letters are supported.
///
/// Examples of valid inputs:
/// fccc, FCCC, #fccc, #FCCC, #ffc1c1c1, #FFC1C1C1, ffc1c1c1, FFC1C1C1
///
/// If the string is not valid, an error is thrown.
///
/// Note: if you are hardcoding colors, use Dart's built-in hexadecimal
/// literals instead.
static Paint fromARGBHexString(String hexString) {
final color = ColorExtension.fromARGBHexString(hexString);
return Paint()..color = color;
}
/// Generates a random [Color] in a new [Paint] object with the set
/// alpha as [withAlpha] or the default (1.0).
/// You can pass in a random number generator [rng], if omitted the function
/// will create a new [Random] object without a seed and use that.
/// [base] can be used to get the random colors in only a lighter spectrum, it
/// should be between [0-256].
static Paint random({
double withAlpha = 1.0,
int base = 0,
Random? rng,
}) {
final color = ColorExtension.random(
withAlpha: withAlpha,
base: base,
rng: rng,
);
return Paint()..color = color;
}
}

View File

@ -1,5 +1,3 @@
import 'dart:math' as math;
import 'package:flame/palette.dart'; import 'package:flame/palette.dart';
import 'package:flame_forge2d/body_component.dart'; import 'package:flame_forge2d/body_component.dart';
import 'package:flame_forge2d/contact_callbacks.dart'; import 'package:flame_forge2d/contact_callbacks.dart';
@ -23,17 +21,7 @@ class Ball extends BodyComponent {
paint = originalPaint; paint = originalPaint;
} }
Paint randomPaint() { Paint randomPaint() => PaintExtension.random(withAlpha: 0.9, base: 100);
final rng = math.Random();
return PaletteEntry(
Color.fromARGB(
100 + rng.nextInt(155),
100 + rng.nextInt(155),
100 + rng.nextInt(155),
255,
),
).paint();
}
@override @override
Body createBody() { Body createBody() {

View File

@ -1,6 +1,5 @@
import 'dart:math';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame/palette.dart'; import 'package:flame/palette.dart';
@ -85,7 +84,7 @@ class Square extends PositionComponent with HasGameRef<RiveExampleGame> {
Square(Vector2 position) { Square(Vector2 position) {
this.position.setFrom(position); this.position.setFrom(position);
size.setValues(100, 100); size.setValues(100, 100);
paint = _randomPaint(); paint = PaintExtension.random(withAlpha: 0.9, base: 100);
} }
@override @override
@ -93,15 +92,4 @@ class Square extends PositionComponent with HasGameRef<RiveExampleGame> {
super.render(canvas); super.render(canvas);
canvas.drawRect(size.toRect(), paint); canvas.drawRect(size.toRect(), paint);
} }
static Paint _randomPaint() {
final rng = Random();
final color = Color.fromRGBO(
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256),
0.9,
);
return PaletteEntry(color).paint();
}
} }