forked from FooStudio/tinycolor
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ricardo Mendieta
committed
Aug 4, 2018
0 parents
commit cbd0916
Showing
11 changed files
with
762 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
.idea | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ | ||
ios/.generated/ | ||
ios/Flutter/Generated.xcconfig | ||
ios/Runner/GeneratedPluginRegistrant.* |
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,3 @@ | ||
## [0.0.1] - TODO: Add release date. | ||
|
||
* TODO: Describe initial release. |
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,7 @@ | ||
Copyright 2018 Foo Studio <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,9 @@ | ||
# tinycolor | ||
|
||
A new Flutter project. | ||
|
||
## Getting Started | ||
|
||
For help getting started with Flutter, view our online [documentation](https://flutter.io/). | ||
|
||
For help on editing package code, view the [documentation](https://flutter.io/developing-packages/). |
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,98 @@ | ||
import 'dart:math' as Math; | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter/painting.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'util.dart'; | ||
|
||
HSL rgbToHsl({@required double r, @required double g, @required double b}) { | ||
r = bound01(r, 255.0); | ||
g = bound01(g, 255.0); | ||
b = bound01(b, 255.0); | ||
|
||
final max = [r, g, b].reduce(Math.max); | ||
final min = [r, g, b].reduce(Math.min); | ||
double h = 0.0; | ||
double s = 0.0; | ||
final double l = (max + min) / 2; | ||
|
||
if (max == min) { | ||
h = s = 0.0; | ||
} else { | ||
final double d = max - min; | ||
s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min); | ||
if (max == r) { | ||
h = (g - b) / d + (g < b ? 6 : 0); | ||
} else if (max == g) { | ||
h = (b - r) / d + 2; | ||
} else if (max == b) { | ||
h = (r - g) / d + 4; | ||
} | ||
} | ||
|
||
h /= 6.0; | ||
return HSL(h: h, s: s, l: l); | ||
} | ||
|
||
Color hslToColor(HSL hsl) { | ||
return hslToRgb(hsl); | ||
} | ||
|
||
Color hslToRgb(HSL hsl) { | ||
double r; | ||
double g; | ||
double b; | ||
final double h = bound01(hsl.h, 360.0); | ||
final double s = bound01(hsl.s * 100, 100.0); | ||
final double l = bound01(hsl.l * 100, 100.0); | ||
|
||
if (s == 0.0) { | ||
r = g = b = l; | ||
} else { | ||
final q = l < 0.5 ? l * (1.0 + s) : l + s - l * s; | ||
final p = 2 * l - q; | ||
r = _hue2rgb(p, q, h + 1 / 3); | ||
g = _hue2rgb(p, q, h); | ||
b = _hue2rgb(p, q, h - 1 / 3); | ||
} | ||
return Color.fromARGB( | ||
hsl.a.round(), (r * 255).round(), (g * 255).round(), (b * 255).round()); | ||
} | ||
|
||
HSVColor colorToHsv(Color color) { | ||
return HSVColor.fromColor(color); | ||
} | ||
|
||
HSVColor rgbToHsv( | ||
{@required int r, @required int g, @required int b, @required int a}) { | ||
return colorToHsv(Color.fromARGB(a, r, g, b)); | ||
} | ||
|
||
Color hsvToColor(HSVColor hsv) { | ||
return hsv.toColor(); | ||
} | ||
|
||
double _hue2rgb(double p, double q, double t) { | ||
if (t < 0) t += 1; | ||
if (t > 1) t -= 1; | ||
if (t < 1 / 6) return p + (q - p) * 6 * t; | ||
if (t < 1 / 2) return q; | ||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; | ||
return p; | ||
} | ||
|
||
class HSL { | ||
double h; | ||
double s; | ||
double l; | ||
double a; | ||
|
||
HSVColor hsvColor; | ||
|
||
HSL({this.h, this.s, this.l, this.a = 0.0}); | ||
|
||
String toString() { | ||
return "HSL(h: $h, s: $s, l: $l, a: $a)"; | ||
} | ||
} |
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,159 @@ | ||
import 'dart:math' as Math; | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter/painting.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:pigment/pigment.dart'; | ||
|
||
import 'conversion.dart'; | ||
import 'util.dart'; | ||
|
||
class TinyColor { | ||
Color originalColor; | ||
Color _color; | ||
|
||
TinyColor(Color color) { | ||
this.originalColor = | ||
Color.fromARGB(color.alpha, color.red, color.green, color.blue); | ||
this._color = | ||
Color.fromARGB(color.alpha, color.red, color.green, color.blue); | ||
} | ||
|
||
factory TinyColor.fromRGB( | ||
{@required int r, @required int g, @required int b, int a = 100}) { | ||
return TinyColor(Color.fromARGB(a, r, g, b)); | ||
} | ||
|
||
factory TinyColor.fromHSL(HSL hsl) { | ||
return TinyColor(hslToColor(hsl)); | ||
} | ||
|
||
factory TinyColor.fromHSV(HSVColor hsv) { | ||
return TinyColor(hsv.toColor()); | ||
} | ||
|
||
factory TinyColor.fromString(String string) { | ||
return TinyColor(Pigment.fromString(string)); | ||
} | ||
|
||
bool isDark() { | ||
return this.getBrightness() < 128.0; | ||
} | ||
|
||
bool isLight() { | ||
return !this.isDark(); | ||
} | ||
|
||
double getBrightness() { | ||
return (_color.red * 299 + _color.green * 587 + _color.blue * 114) / 1000; | ||
} | ||
|
||
double getLuminance() { | ||
return _color.computeLuminance(); | ||
} | ||
|
||
TinyColor setAlpha(int alpha) { | ||
_color.withAlpha(alpha); | ||
return this; | ||
} | ||
|
||
TinyColor setOpacity(double opacity) { | ||
_color.withOpacity(opacity); | ||
return this; | ||
} | ||
|
||
HSVColor toHsv() { | ||
return colorToHsv(_color); | ||
} | ||
|
||
HSL toHsl() { | ||
final hsl = rgbToHsl( | ||
r: _color.red.toDouble(), | ||
g: _color.green.toDouble(), | ||
b: _color.blue.toDouble(), | ||
); | ||
return HSL(h: hsl.h * 360, s: hsl.s, l: hsl.l, a: _color.alpha.toDouble()); | ||
} | ||
|
||
TinyColor clone() { | ||
return TinyColor(_color); | ||
} | ||
|
||
TinyColor lighten([int amount = 10]) { | ||
final hsl = this.toHsl(); | ||
hsl.l += amount / 100; | ||
hsl.l = clamp01(hsl.l); | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
TinyColor brighten([int amount = 10]) { | ||
final color = Color.fromARGB( | ||
_color.alpha, | ||
Math.max(0, Math.min(255, _color.red - (255 * -(amount / 100)).round())), | ||
Math.max( | ||
0, Math.min(255, _color.green - (255 * -(amount / 100)).round())), | ||
Math.max(0, Math.min(255, _color.blue - (255 * -(amount / 100)).round())), | ||
); | ||
return TinyColor(color); | ||
} | ||
|
||
TinyColor darken([int amount = 10]) { | ||
final hsl = this.toHsl(); | ||
hsl.l -= amount / 100; | ||
hsl.l = clamp01(hsl.l); | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
TinyColor tint([int amount = 10]) { | ||
return this.mix(input: Color.fromRGBO(255, 255, 255, 1.0)); | ||
} | ||
|
||
TinyColor shade([int amount = 10]) { | ||
return this.mix(input: Color.fromRGBO(0, 0, 0, 1.0)); | ||
} | ||
|
||
TinyColor desaturate([int amount = 10]) { | ||
final hsl = this.toHsl(); | ||
hsl.s -= amount / 100; | ||
hsl.s = clamp01(hsl.s); | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
TinyColor saturate([int amount = 10]) { | ||
final hsl = this.toHsl(); | ||
hsl.s += amount / 100; | ||
hsl.s = clamp01(hsl.s); | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
TinyColor greyscale() { | ||
return desaturate(100); | ||
} | ||
|
||
TinyColor spin(double amount) { | ||
final hsl = this.toHsl(); | ||
final hue = (hsl.h + amount) % 360; | ||
hsl.h = hue < 0 ? 360 + hue : hue; | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
TinyColor mix({@required Color input, int amount = 50}) { | ||
final int p = (amount / 100).round(); | ||
final color = Color.fromARGB( | ||
(input.alpha - _color.alpha) * p + _color.alpha, | ||
(input.red - _color.red) * p + _color.red, | ||
(input.green - _color.green) * p + _color.green, | ||
(input.blue - _color.blue) * p + _color.blue); | ||
return TinyColor(color); | ||
} | ||
|
||
TinyColor complement() { | ||
final hsl = this.toHsl(); | ||
hsl.h = (hsl.h + 180) % 360; | ||
return TinyColor.fromHSL(hsl); | ||
} | ||
|
||
Color get color { | ||
return _color; | ||
} | ||
} |
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,21 @@ | ||
import 'dart:core'; | ||
import 'dart:math' as Math; | ||
|
||
double bound01(double n, double max) { | ||
n = max == 360.0 ? n : Math.min(max, Math.max(0.0, n)); | ||
final double absDifference = n - max; | ||
if (absDifference.abs() < 0.000001) { | ||
return 1.0; | ||
} | ||
|
||
if (max == 360) { | ||
n = (n < 0 ? n % max + max : n % max) / max; | ||
} else { | ||
n = (n % max) / max; | ||
} | ||
return n; | ||
} | ||
|
||
double clamp01(double val) { | ||
return Math.min(1.0, Math.max(0.0, val)); | ||
} |
Oops, something went wrong.