diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9172bce --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +.dart_tool/ +.idea + +.packages +.pub/ + +build/ +ios/.generated/ +ios/Flutter/Generated.xcconfig +ios/Runner/GeneratedPluginRegistrant.* diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ac07159 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## [0.0.1] - TODO: Add release date. + +* TODO: Describe initial release. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1b969c1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2018 Foo Studio + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fe3f03a --- /dev/null +++ b/README.md @@ -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/). diff --git a/lib/conversion.dart b/lib/conversion.dart new file mode 100644 index 0000000..c523ffb --- /dev/null +++ b/lib/conversion.dart @@ -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)"; + } +} diff --git a/lib/tinycolor.dart b/lib/tinycolor.dart new file mode 100644 index 0000000..ab710d4 --- /dev/null +++ b/lib/tinycolor.dart @@ -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; + } +} diff --git a/lib/util.dart b/lib/util.dart new file mode 100644 index 0000000..c23393c --- /dev/null +++ b/lib/util.dart @@ -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)); +} diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..cc57590 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,370 @@ +# Generated by pub +# See https://www.dartlang.org/tools/pub/glossary#lockfile +packages: + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "0.31.2-alpha.2" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.3" + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.7" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.14.6" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.4" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + front_end: + dependency: transitive + description: + name: front_end + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0-alpha.12" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.5" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.3" + http: + dependency: transitive + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.11.3+16" + http_multi_server: + dependency: transitive + description: + name: http_multi_server + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.2" + io: + dependency: transitive + description: + name: io + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.2+1" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.1" + kernel: + dependency: transitive + description: + name: kernel + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.0-alpha.12" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "0.11.3+1" + matcher: + dependency: transitive + description: + name: matcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.2+1" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.5" + mime: + dependency: transitive + description: + name: mime + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.6" + multi_server_socket: + dependency: transitive + description: + name: multi_server_socket + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + node_preamble: + dependency: transitive + description: + name: node_preamble + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.1" + package_config: + dependency: transitive + description: + name: package_config + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + package_resolver: + dependency: transitive + description: + name: package_resolver + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.1" + pigment: + dependency: "direct main" + description: + name: pigment + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" + plugin: + dependency: transitive + description: + name: plugin + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0+2" + pool: + dependency: transitive + description: + name: pool + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.4" + pub_semver: + dependency: transitive + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.1" + quiver: + dependency: transitive + description: + name: quiver + url: "https://pub.dartlang.org" + source: hosted + version: "0.29.0+1" + shelf: + dependency: transitive + description: + name: shelf + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.3" + shelf_packages_handler: + dependency: transitive + description: + name: shelf_packages_handler + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + shelf_static: + dependency: transitive + description: + name: shelf_static + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.7" + shelf_web_socket: + dependency: transitive + description: + name: shelf_web_socket + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.2" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_map_stack_trace: + dependency: transitive + description: + name: source_map_stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.4" + source_maps: + dependency: transitive + description: + name: source_maps + url: "https://pub.dartlang.org" + source: hosted + version: "0.10.5" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.9.2" + stream_channel: + dependency: transitive + description: + name: stream_channel + url: "https://pub.dartlang.org" + source: hosted + version: "1.6.6" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + test: + dependency: transitive + description: + name: test + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.37" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.5" + utf: + dependency: transitive + description: + name: utf + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.0+4" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.6" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.7+7" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.7" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.13" +sdks: + dart: ">=2.0.0-dev.52.0 <=2.0.0-dev.58.0.flutter-f981f09760" diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..f2cce7f --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,51 @@ +name: tinycolor +description: Flutter Color manipulation and conversion +version: 1.0.0 +author: Foo() +homepage: https://github.com/FooStudio/tinycolor + +dependencies: + pigment: ^0.1.3 + flutter: + sdk: flutter + +dev_dependencies: + flutter_test: + sdk: flutter + +# For information on the generic Dart part of this file, see the +# following page: https://www.dartlang.org/tools/pub/pubspec + +# The following section is specific to Flutter. +flutter: + + # To add assets to your package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.io/assets-and-images/#from-packages + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.io/assets-and-images/#resolution-aware. + + # To add custom fonts to your package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.io/custom-fonts/#from-packages diff --git a/test/util_test.dart b/test/util_test.dart new file mode 100644 index 0000000..2a68322 --- /dev/null +++ b/test/util_test.dart @@ -0,0 +1,19 @@ +import 'package:test/test.dart'; +import 'package:tinycolor/util.dart'; + +void main() { + test("bound01 values", () { + expect(bound01(0.5, 1.0), 0.5); + expect(bound01(50.0, 100.0), 0.5); + expect(bound01(20.0, 200.0), 0.1); + expect(bound01(25.0, 75.0), 0.3333333333333333); + }); + + test("clamp01 values", () { + expect(clamp01(10.0), 1.0); + expect(clamp01(0.5), 0.5); + expect(clamp01(1.5), 1.0); + expect(clamp01(0.75), 0.75); + expect(clamp01(1.1), 1.0); + }); +} diff --git a/tinycolor.iml b/tinycolor.iml new file mode 100644 index 0000000..a461e81 --- /dev/null +++ b/tinycolor.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file