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