forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Color and Image extensions (flame-engine#653)
Co-authored-by: Erick <[email protected]>
- Loading branch information
1 parent
b1fa449
commit ac40b2c
Showing
5 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'dart:ui'; | ||
|
||
export 'dart:ui' show Color; | ||
|
||
extension ColorExtension on Color { | ||
/// Darken the shade of the color by the [amount]. | ||
/// | ||
/// [amount] is a double between 0 and 1. | ||
/// | ||
/// Based on: https://stackoverflow.com/a/60191441. | ||
Color darken(double amount) { | ||
assert(amount >= 0 && amount <= 1); | ||
|
||
final f = 1 - amount; | ||
return Color.fromARGB( | ||
alpha, | ||
(red * f).round(), | ||
(green * f).round(), | ||
(blue * f).round(), | ||
); | ||
} | ||
|
||
/// Brighten the shade of the color by the [amount]. | ||
/// | ||
/// [amount] is a double between 0 and 1. | ||
/// | ||
/// Based on: https://stackoverflow.com/a/60191441. | ||
Color brighten(double amount) { | ||
assert(amount >= 0 && amount <= 1); | ||
|
||
return Color.fromARGB( | ||
alpha, | ||
red + ((255 - red) * amount).round(), | ||
green + ((255 - green) * amount).round(), | ||
blue + ((255 - blue) * amount).round(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'dart:typed_data'; | ||
import 'dart:ui'; | ||
|
||
import '../flame.dart'; | ||
import 'color.dart'; | ||
|
||
export 'dart:ui' show Image; | ||
|
||
extension ImageExtension on Image { | ||
/// Helper method for retrieve the pixel data in a Uint8 format. | ||
/// | ||
/// Pixel order used the [ImageByteFormat.rawRgba] meaning it is: R G B A. | ||
Future<Uint8List> pixelsInUint8() async { | ||
return (await toByteData()).buffer.asUint8List(); | ||
} | ||
|
||
/// Returns the bounding [Rect] of the image. | ||
Rect getBoundingRect() { | ||
return Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble()); | ||
} | ||
|
||
/// Change each pixel's color to be darker and return a new [Image]. | ||
/// | ||
/// The [amount] is a double value between 0 and 1. | ||
Future<Image> darken(double amount) async { | ||
assert(amount >= 0 && amount <= 1); | ||
|
||
final pixelData = await pixelsInUint8(); | ||
final newPixelData = Uint8List(pixelData.length); | ||
|
||
for (var i = 0; i < pixelData.length; i += 4) { | ||
final color = Color.fromARGB( | ||
pixelData[i + 3], | ||
pixelData[i + 0], | ||
pixelData[i + 1], | ||
pixelData[i + 2], | ||
).darken(amount); | ||
|
||
newPixelData[i] = color.red; | ||
newPixelData[i + 1] = color.green; | ||
newPixelData[i + 2] = color.blue; | ||
newPixelData[i + 3] = color.alpha; | ||
} | ||
|
||
return Flame.images.decodeImageFromPixels(newPixelData, width, height); | ||
} | ||
|
||
/// Change each pixel's color to be brighter and return a new [Image]. | ||
/// | ||
/// The [amount] is a double value between 0 and 1. | ||
Future<Image> brighten(double amount) async { | ||
assert(amount >= 0 && amount <= 1); | ||
|
||
final pixelData = await pixelsInUint8(); | ||
final newPixelData = Uint8List(pixelData.length); | ||
|
||
for (var i = 0; i < pixelData.length; i += 4) { | ||
final color = Color.fromARGB( | ||
pixelData[i + 3], | ||
pixelData[i + 0], | ||
pixelData[i + 1], | ||
pixelData[i + 2], | ||
).brighten(amount); | ||
|
||
newPixelData[i] = color.red; | ||
newPixelData[i + 1] = color.green; | ||
newPixelData[i + 2] = color.blue; | ||
newPixelData[i + 3] = color.alpha; | ||
} | ||
|
||
return Flame.images.decodeImageFromPixels(newPixelData, width, height); | ||
} | ||
} |