Make colors more accessible

Using a combination of the HSLuv (Pastel) color space and incrementing by the Golden Angle
This commit is contained in:
Daniel Martin
2023-07-22 14:27:48 +10:00
parent 852decbe7d
commit b27bdc63c1

View File

@@ -1,5 +1,6 @@
import 'dart:math';
import 'package:hsluv/hsluv.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:obtainium/components/generated_form_modal.dart';
@@ -132,19 +133,19 @@ class GeneratedForm extends StatefulWidget {
State<GeneratedForm> createState() => _GeneratedFormState();
}
// Generates a random light color
// Courtesy of ChatGPT 😭 (with a bugfix 🥳)
// Generates a color in the HSLuv (Pastel) color space
// https://pub.dev/documentation/hsluv/latest/hsluv/Hsluv/hpluvToRgb.html
Color generateRandomLightColor() {
// Create a random number generator
final Random random = Random();
// Generate random hue, saturation, and value values
final double hue = random.nextDouble() * 360;
final double saturation = 0.5 + random.nextDouble() * 0.5;
final double value = 0.9 + random.nextDouble() * 0.1;
// Create a HSV color with the random values
return HSVColor.fromAHSV(1.0, hue, saturation, value).toColor();
final randomSeed = Random().nextInt(120);
// https://en.wikipedia.org/wiki/Golden_angle
final goldenAngle = 180 * (3 - sqrt(5));
// Generate next golden angle hue
final double hue = randomSeed * goldenAngle;
// Map from HPLuv color space to RGB, use constant saturation=100, lightness=70
final List<double> rgbValuesDbl = Hsluv.hpluvToRgb([hue, 100, 70]);
// Map RBG values from 0-1 to 0-255:
final List<int> rgbValues = rgbValuesDbl.map((rgb) => (rgb * 255).toInt()).toList();
return Color.fromARGB(255, rgbValues[0], rgbValues[1], rgbValues[2]);
}
class _GeneratedFormState extends State<GeneratedForm> {