Skip to content

Instantly share code, notes, and snippets.

@s0nerik
Last active April 24, 2020 18:34
Show Gist options
  • Save s0nerik/11eeb412f55249ba61a482392f6601d7 to your computer and use it in GitHub Desktop.
Save s0nerik/11eeb412f55249ba61a482392f6601d7 to your computer and use it in GitHub Desktop.
Color calc
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: MyWidget(),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
Color _testColor;
Color _testGradientStart;
Color _testGradientEnd;
List<Color> get _testGradient => [_testGradientStart, _testGradientEnd];
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Color',
),
onChanged: (colorStr) => setState(() {
_testColor = _parseColor(colorStr);
}),
),
),
const SizedBox(width: 16),
Container(width: 48, height: 48, color: _testColor),
],
),
const SizedBox(height: 16),
Text('Color brightness: ${getBrightness(_testColor)}'),
Text('Color is ${isDark(_testColor) ? 'DARK' : 'LIGHT'}'),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Gradient start color',
),
onChanged: (colorStr) => setState(() {
_testGradientStart = _parseColor(colorStr);
}),
),
),
const SizedBox(width: 16),
Container(width: 48, height: 48, color: _testGradient.first),
],
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Gradient start color',
),
onChanged: (colorStr) => setState(() {
_testGradientEnd = _parseColor(colorStr);
}),
),
),
const SizedBox(width: 16),
Container(width: 48, height: 48, color: _testGradient.last),
],
),
const SizedBox(height: 16),
Row(
children: [
Text('Gradient average color: '),
Container(
width: 16, height: 16, color: getAverageColor(_testGradient)),
],
),
Text(
'Gradient brightness: ${getBrightness(getAverageColor(_testGradient))}'),
Text('Gradient is ${isDark(getAverageColor(_testGradient)) ? 'DARK' : 'LIGHT'}'),
],
),
);
}
Color _parseColor(String colorStr) {
String hexColor =
colorStr?.toUpperCase()?.replaceAll("#", "")?.replaceAll("0X", "");
if (hexColor?.length != 6 && hexColor?.length != 8) {
return null;
}
if (hexColor != null && hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
try {
return Color(int.parse(hexColor, radix: 16));
} catch (_) {
return null;
}
}
}
bool isDark(Color color) {
if (color == null) {
return false;
}
return getBrightness(color) < 128.0;
}
double getBrightness(Color color) {
if (color == null) {
return 0;
}
return (color.red * 299 + color.green * 587 + color.blue * 114) / 1000;
}
Color getAverageColor(List<Color> colors) {
if (colors == null || colors.isEmpty || colors.contains(null)) {
return null;
}
double r = 0;
double g = 0;
double b = 0;
for (final c in colors) {
r += c.red * c.red;
g += c.green * c.green;
b += c.blue * c.blue;
}
return Color.fromARGB(
255,
sqrt(r / colors.length).round(),
sqrt(g / colors.length).round(),
sqrt(b / colors.length).round(),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment