mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-10-22 02:13:47 +02:00
Merge pull request #703 from djm2k/feature/accessible-random-colors
Fix #397 Edit Category Colors & Accessible colors using HSLuv
This commit is contained in:
@@ -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> {
|
||||||
@@ -368,6 +369,36 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
|||||||
));
|
));
|
||||||
}) ??
|
}) ??
|
||||||
[const SizedBox.shrink()],
|
[const SizedBox.shrink()],
|
||||||
|
(values[widget.items[r][e].key]
|
||||||
|
as Map<String, MapEntry<int, bool>>?)
|
||||||
|
?.values
|
||||||
|
.where((e) => e.value)
|
||||||
|
.length == 1
|
||||||
|
? Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||||
|
child: IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
var temp = values[widget.items[r][e].key]
|
||||||
|
as Map<String, MapEntry<int, bool>>;
|
||||||
|
// get selected category str where bool is true
|
||||||
|
final oldEntry = temp.entries.firstWhere((entry) => entry.value.value);
|
||||||
|
// generate new color, ensure it is not the same
|
||||||
|
int newColor = oldEntry.value.key;
|
||||||
|
while(oldEntry.value.key == newColor) {
|
||||||
|
newColor = generateRandomLightColor().value;
|
||||||
|
}
|
||||||
|
// Update entry with new color, remain selected
|
||||||
|
temp.update(oldEntry.key, (old) => MapEntry(newColor, old.value));
|
||||||
|
values[widget.items[r][e].key] = temp;
|
||||||
|
someValueChanged();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.format_color_fill_rounded),
|
||||||
|
visualDensity: VisualDensity.compact,
|
||||||
|
tooltip: tr('colour'),
|
||||||
|
))
|
||||||
|
: const SizedBox.shrink(),
|
||||||
(values[widget.items[r][e].key]
|
(values[widget.items[r][e].key]
|
||||||
as Map<String, MapEntry<int, bool>>?)
|
as Map<String, MapEntry<int, bool>>?)
|
||||||
?.values
|
?.values
|
||||||
|
@@ -1,5 +1,3 @@
|
|||||||
import 'dart:math';
|
|
||||||
|
|
||||||
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/custom_app_bar.dart';
|
import 'package:obtainium/components/custom_app_bar.dart';
|
||||||
@@ -21,21 +19,6 @@ class SettingsPage extends StatefulWidget {
|
|||||||
State<SettingsPage> createState() => _SettingsPageState();
|
State<SettingsPage> createState() => _SettingsPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a random light color
|
|
||||||
// Courtesy of ChatGPT 😭 (with a bugfix 🥳)
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _SettingsPageState extends State<SettingsPage> {
|
class _SettingsPageState extends State<SettingsPage> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@@ -326,6 +326,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "8.2.2"
|
version: "8.2.2"
|
||||||
|
hsluv:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: hsluv
|
||||||
|
sha256: f33e63b0c24ceee0f6492874424aa8edc671ef9a20cc889e4b969284d8f02eb1
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.3"
|
||||||
html:
|
html:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@@ -64,7 +64,7 @@ dependencies:
|
|||||||
android_intent_plus: ^4.0.0
|
android_intent_plus: ^4.0.0
|
||||||
flutter_markdown: ^0.6.14
|
flutter_markdown: ^0.6.14
|
||||||
flutter_archive: ^5.0.0
|
flutter_archive: ^5.0.0
|
||||||
|
hsluv: ^1.1.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Reference in New Issue
Block a user