prism 2.0.0-beta.1
prism: ^2.0.0-beta.1 copied to clipboard
A powerful and efficient color manipulation library for Dart and Flutter applications. Features Ray class, RayScheme accessibility tools, and extensive color palettes.
example/example.dart
import 'package:prism/prism.dart';
import 'package:prism/palettes/rgb/css.dart';
import 'package:prism/palettes/rgb/material.dart';
void main() {
print('π Prism Library Demo\n');
// === Color Creation ===
print('π¨ Creating Colors:');
final rgbRed = RayRgb8.fromHex('#FF0000');
final hslGreen = RayHsl(hue: 120, saturation: 1.0, lightness: 0.5);
final oklabBlue = RayOklab(l: 0.452, a: -0.032, b: -0.312);
final oklchPurple = RayOklch(l: 0.6, c: 0.2, h: 300);
print('RGB Red: ${rgbRed.toHexStr()}');
print('HSL Green: ${hslGreen.toRgb().toHexStr()}');
print('Oklab Blue: ${oklabBlue.toRgb().toHexStr()}');
print('Oklch Purple: ${oklchPurple.toRgb().toHexStr()}\n');
// === Seamless Conversion ===
print('π Color Model Conversion:');
final baseColor = RayRgb8.fromHex('#FF6B35'); // Orange
print('Original RGB: ${baseColor.toHexStr()}');
print('β HSL: ${baseColor.toHsl()}');
print('β Oklab: ${baseColor.toOklab()}');
print('β Oklch: ${baseColor.toOklch()}');
print('Round-trip: ${baseColor.toHsl().toRgb().toHexStr()}\n');
// === Color Manipulation ===
print('π§ Color Manipulation:');
final red = RayRgb8.fromHex('#E53E3E');
final blue = RayRgb8.fromHex('#3182CE');
print('Original: ${red.toHexStr()}');
print('50% Opacity: ${red.withOpacity(0.5).toHexStr(8)}');
print('Interpolated (RedβBlue): ${red.lerp(blue, 0.5).toHexStr()}');
print('Inverted: ${red.inverse.toHexStr()}');
// HSL manipulation
final hslColor = red.toHsl();
print(
'Hue shifted (+60Β°): ${hslColor.withHue(hslColor.hue + 60).toRgb().toHexStr()}');
// Oklch manipulation (perceptually uniform)
final oklchColor = red.toOklch();
print(
'Darker (perceptual): ${oklchColor.withLightness(0.3).toRgb().toHexStr()}\n');
// === Accessibility Schemes ===
print('βΏ Accessibility Schemes:');
final colors = [
RayRgb8.fromHex('#2D3748'), // Dark gray
RayRgb8.fromHex('#E53E3E'), // Red
RayRgb8.fromHex('#38A169'), // Green
RayRgb8.fromHex('#3182CE'), // Blue
];
for (final color in colors) {
final scheme = RayScheme.fromRay(color);
final theme = scheme.source.isDark ? 'Dark' : 'Light';
print('${color.toHexStr()} β $theme theme');
print(' Text color: ${scheme.source.onRay.toHexStr()}');
print(' Light surface: ${scheme.surfaceLight.toHexStr()}');
print(' Dark surface: ${scheme.surfaceDark.toHexStr()}');
}
print('');
// === Color Palettes ===
print('π¨ Color Palettes:');
// CSS Colors
print('CSS Palette:');
print(' Navy: ${CssRgb.navy.source.toHexStr()}');
print(' Crimson: ${CssRgb.crimson.source.toHexStr()}');
print(' Gold: ${CssRgb.gold.source.toHexStr()}');
// Material Design
print('Material Design:');
print(' Blue 500: ${MaterialRgb.blue.shade500?.toHexStr() ?? "N/A"}');
print(' Red 700: ${MaterialRgb.red.shade700?.toHexStr() ?? "N/A"}');
print(' Green A200: ${MaterialRgb.green.accent200?.toHexStr() ?? "N/A"}');
print('');
// === Advanced Features ===
print('π§ͺ Advanced Features:');
// Perceptual interpolation comparison
final startRgb = RayRgb8.fromHex('#FF0000'); // Red
final endRgb = RayRgb8.fromHex('#00FF00'); // Green
final rgbMidpoint = startRgb.lerp(endRgb, 0.5);
final oklabMidpoint = startRgb.toOklab().lerp(endRgb.toOklab(), 0.5).toRgb();
print('RGB Interpolation: ${rgbMidpoint.toHexStr()}');
print('Oklab Interpolation: ${oklabMidpoint.toHexStr()}');
// Color analysis
print('\nColor Analysis:');
final testColor = RayRgb8.fromHex('#4A90E2');
print('Color: ${testColor.toHexStr()}');
print('Luminance: ${testColor.computeLuminance().toStringAsFixed(3)}');
final bestContrast = testColor.maxContrast(
RayRgb8.fromHex('#000000'), RayRgb8.fromHex('#FFFFFF')) as RayRgb8;
print('Best contrast: ${bestContrast.toHexStr()}');
// HSL color distance
final color1 = RayHsl(hue: 30, saturation: 0.8, lightness: 0.6);
final color2 = RayHsl(hue: 150, saturation: 0.6, lightness: 0.4);
print('Hue distance: ${color1.hueDistance(color2).toStringAsFixed(1)}Β°');
print('\nβ
Demo completed successfully!');
}