Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/parable-bloom/lib/core/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ class AppTheme {
/// Returns ratio >= 4.5 for normal text, >= 3.0 for large text
static double getContrastRatio(Color foreground, Color background) {
double luminance(Color color) {
final r = color.r / 255.0;
final g = color.g / 255.0;
final b = color.b / 255.0;
final r = color.r;
final g = color.g;
final b = color.b;
final rsRGB = r <= 0.03928 ? r / 12.92 : pow((r + 0.055) / 1.055, 2.4);
final gsRGB = g <= 0.03928 ? g / 12.92 : pow((g + 0.055) / 1.055, 2.4);
final bsRGB = b <= 0.03928 ? b / 12.92 : pow((b + 0.055) / 1.055, 2.4);
Expand Down
71 changes: 71 additions & 0 deletions apps/parable-bloom/test/core/app_theme_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:parable_bloom/core/app_theme.dart';

void main() {
group('AppTheme.getContrastRatio Tests', () {
test('Pure White vs. Pure Black should have maximum contrast (21.0)', () {
final white = Colors.white; // 0xFFFFFFFF
final black = Colors.black; // 0xFF000000

final contrast = AppTheme.getContrastRatio(white, black);
expect(contrast, closeTo(21.0, 0.01));
});

test('Same colors should have minimum contrast (1.0)', () {
final white = Colors.white;
final black = Colors.black;
final customColor = const Color(0xFF3A7DAF);

expect(AppTheme.getContrastRatio(white, white), closeTo(1.0, 0.01));
expect(AppTheme.getContrastRatio(black, black), closeTo(1.0, 0.01));
expect(AppTheme.getContrastRatio(customColor, customColor),
closeTo(1.0, 0.01));
});

test('getContrastRatio is commutative', () {
final primary = const Color(0xFF3A7DAF);
final background = const Color(0xFFF8F5EF);

final ratio1 = AppTheme.getContrastRatio(primary, background);
final ratio2 = AppTheme.getContrastRatio(background, primary);

expect(ratio1, equals(ratio2));
});

test('Colors below/above linearization threshold are calculated correctly',
() {
// Linearization condition: c / 255.0 <= 0.03928
// Threshold in 255 range: 0.03928 * 255.0 = 10.0164
// So color channel values <= 10 use standard linearization: (c/255.0) / 12.92
// Values >= 11 use non-linear formula: pow(((c/255.0) + 0.055) / 1.055, 2.4)

const veryDarkGray = Color(0xFF090909); // Red, Green, Blue are 9 (<= 10)
const slightlyLighterGray =
Color(0xFF0B0B0B); // Red, Green, Blue are 11 (>= 11)

// Both should work and should result in slightly different contrast against white
final white = Colors.white;

final ratioDark = AppTheme.getContrastRatio(veryDarkGray, white);
final ratioLighter =
AppTheme.getContrastRatio(slightlyLighterGray, white);

// We expect dark grey to have slightly higher contrast ratio against white than slightly lighter grey
expect(ratioDark, greaterThan(ratioLighter));
});

test(
'Calculates expected contrast ratios for known WCAG compliancy thresholds',
() {
// Verify contrast ratio of specific palette colors
final primary = const Color(0xFF3A7DAF); // Brand primary blue
final lightBg = const Color(0xFFF8F5EF); // Lightened beige background

final ratio = AppTheme.getContrastRatio(primary, lightBg);

// Known contrast ratio for #3A7DAF vs #F8F5EF is approx 4.08
expect(ratio, closeTo(4.08, 0.05));
});
});
}
Loading