From b27bdc63c1787b88351772906838dfe82495b22b Mon Sep 17 00:00:00 2001 From: Daniel Martin Date: Sat, 22 Jul 2023 14:27:48 +1000 Subject: [PATCH] Make colors more accessible Using a combination of the HSLuv (Pastel) color space and incrementing by the Golden Angle --- lib/components/generated_form.dart | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/components/generated_form.dart b/lib/components/generated_form.dart index 8ab4149..761d6bc 100644 --- a/lib/components/generated_form.dart +++ b/lib/components/generated_form.dart @@ -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 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 rgbValuesDbl = Hsluv.hpluvToRgb([hue, 100, 70]); + // Map RBG values from 0-1 to 0-255: + final List rgbValues = rgbValuesDbl.map((rgb) => (rgb * 255).toInt()).toList(); + return Color.fromARGB(255, rgbValues[0], rgbValues[1], rgbValues[2]); } class _GeneratedFormState extends State {