Skip to content

Commit

Permalink
Refactor random true color generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TechAurelian committed Oct 2, 2024
1 parent 2d28809 commit 0f7dfa0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 65 deletions.
24 changes: 1 addition & 23 deletions lib/models/random_color_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ final Random _random = _createRandom();
/// mixed random colors.
final List<ColorType> _typeWeights = _initTypeWeights();

/// A blacklist of all the codes of the basic, web & named colors, used to avoid generating true
/// colors that are already in these lists.
final Set<int> _knownColorsBlacklist = _initKnownColorsBlacklist();

/// Creates a secure or insecure random number generator depending on the given [secure] parameter.
Random _createRandom({bool secure = true}) {
Random random;
Expand Down Expand Up @@ -54,24 +50,6 @@ List<ColorType> _initTypeWeights() {
return typeWeights;
}

/// Creates the blacklist of all the codes of the basic, web & named colors.
///
/// This is used to avoid generating true colors that are already in these lists.
Set<int> _initKnownColorsBlacklist() {
final Set<int> colorCodes = <int>{};
for (final KnownColor color in rbcg.kBasicColors) {
colorCodes.add(color.code);
}
for (final KnownColor color in rwcg.kWebColors) {
colorCodes.add(color.code);
}
for (final KnownColor color in rncg.kNamedColors) {
colorCodes.add(color.code);
}
print('colorCodes length: ${colorCodes.length}');
return colorCodes;
}

/// Generates a random color based on the given color type.
///
/// The returned [RandomColor] has a name if it was generated from named colors, or if its color
Expand All @@ -89,7 +67,7 @@ RandomColor nextRandomColor(ColorType colorType) {
case ColorType.attractiveColor:
return racg.nextRandomColor(_random);
case ColorType.trueColor:
return rtcg.nextRandomColor(_random, blacklist: _knownColorsBlacklist);
return rtcg.nextRandomColor(_random);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,19 @@ import '../../utils/color_utils.dart' as color_utils;
import '../color_type.dart';
import '../random_color.dart';

int _trueColorCount = 0;

/// Generates a random true color.
RandomColor nextRandomColor(Random random, {required Set<int> blacklist}) {
late int randomColorCode;

_trueColorCount++;
print(_trueColorCount);

// Generate a random true color that is not in the black list (the basic, web & named colors)
// To avoid infinite loop, let's try 1000 times, it should be more than enough because the black
// list is less than 2K colors, while the total number of true colors is 16M.
for (int i = 0; i < 1000; i++) {
// Generate a random 24-bit color code, which is a number between 0x000000 and 0xFFFFFF
randomColorCode = color_utils.withFullAlpha(random.nextInt(0xFFFFFF + 1));
// randomColorCode = color_utils.withFullAlpha(random.nextInt(0xFF + 1));
// randomColorCode = random.nextInt(0xFFFFFF + 1);

// print('randomColorCode $i: $randomColorCode');
if (i > 0) {
print(
'$randomColorCode: We tried $i times to generate a random true color that is not in the black list.');
}

if (!blacklist.contains(randomColorCode)) {
break;
}
}
// print('');
RandomColor nextRandomColor(Random random) {
// Generate a random 24-bit color code, which is a number between 0x000000 and 0xFFFFFF
final int randomColorCode = random.nextInt(0xFFFFFF + 1);

return RandomColor(
type: ColorType.trueColor,
color: Color(randomColorCode),
color: Color(color_utils.withFullAlpha(randomColorCode)),
name: null,
listPosition: randomColorCode,
);
}

// /// Generates a random true color.
// RandomColor nextRandomColor(Random random) {
// // Generate a random 24-bit color code, which is a number between 0x000000 and 0xFFFFFF
// final int randomColorCode = random.nextInt(0xFFFFFF + 1);

// return RandomColor(
// type: ColorType.trueColor,
// color: Color(color_utils.withFullAlpha(randomColorCode)),
// name: null,
// listPosition: randomColorCode,
// );
// }

/// The number of available true colors that can be used to generate the random color.
///
/// This is the total number of colors can be represented in a 24-bit color palette.
Expand Down

0 comments on commit 0f7dfa0

Please sign in to comment.