{"id":26349588,"url":"https://github.com/willibrandon/accessiblecolors","last_synced_at":"2025-03-16T09:18:53.677Z","repository":{"id":266952573,"uuid":"899856594","full_name":"willibrandon/AccessibleColors","owner":"willibrandon","description":"O(1) WCAG-Compliant Contrast Colors for Inclusive UI Design","archived":false,"fork":false,"pushed_at":"2024-12-12T01:37:35.000Z","size":342,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-11T07:23:05.261Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willibrandon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-07T07:36:43.000Z","updated_at":"2025-01-28T07:17:19.000Z","dependencies_parsed_at":"2024-12-07T08:34:23.005Z","dependency_job_id":null,"html_url":"https://github.com/willibrandon/AccessibleColors","commit_stats":null,"previous_names":["willibrandon/accessiblecolors"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willibrandon%2FAccessibleColors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willibrandon%2FAccessibleColors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willibrandon%2FAccessibleColors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willibrandon%2FAccessibleColors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willibrandon","download_url":"https://codeload.github.com/willibrandon/AccessibleColors/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243847364,"owners_count":20357362,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-16T09:18:53.069Z","updated_at":"2025-03-16T09:18:53.665Z","avatar_url":"https://github.com/willibrandon.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AccessibleColors\n\n[![NuGet](https://img.shields.io/nuget/v/AccessibleColors.svg?label=NuGet)](https://www.nuget.org/packages/AccessibleColors/)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/AccessibleColors.svg)](https://www.nuget.org/packages/AccessibleColors/)\n[![Build](https://github.com/willibrandon/AccessibleColors/actions/workflows/ci.yml/badge.svg)](https://github.com/willibrandon/AccessibleColors/actions/workflows/ci.yml)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n**AccessibleColors** is a lightweight C# library that helps you ensure that your application's colors meet the WCAG 2.2 contrast ratios. It instantly determines a suitable, compliant foreground color for any given background color and provides tools to verify and generate accessible color palettes.\n\nKey functionalities include:\n\n- **Instant WCAG-Compliant Foregrounds**: Quickly get a foreground color that achieves at least a 4.5:1 contrast on normal text, or 3:1 for large text/UI elements.\n- **Accessible Color Ramps**: Generate a sequence of related, contrast-compliant colors (\"ramps\") derived from a single base color. These ramps are useful for theming states (hover, pressed, disabled) or for ensuring multiple UI components share a visual theme while remaining accessible.\n\n## Why AccessibleColors?\n\nEnsuring accessibility compliance can be tedious. AccessibleColors automates contrast checking and selection, saving you from guesswork and manual tweaking. Whether you're implementing dark mode, creating brand-consistent themes, or just need to verify WCAG compliance, this library provides a straightforward, dependency-free solution.\n\n## Key Features\n\n- **WCAG Compliance**: Guarantees at least a 4.5:1 contrast ratio for normal text and supports 3:1 for large text/UI elements.\n- **O(1) Single-Color Calculations**: Uses a precomputed LUT for sRGB-to-linear conversions, ensuring instant performance for single-color queries.\n- **Accessible Ramps for Theming**:\n  - **GenerateAccessibleRamp**: Produces a series of related colors (a \"ramp\") that remain accessible against a chosen background (dark or light).\n  - **Already-Compliant Base Colors**: If the base color already meets WCAG standards, no adjustments are needed, resulting in little or no visible changes.\n  - **Non-Compliant Base Colors**: If the base color is not compliant, each step is adjusted to ensure compliance, producing a clear gradient of accessible shades.\n- **No External Dependencies**: Works directly with `System.Drawing`.\n- **Simple API**:\n  - **`GetContrastColor`**: Instantly find a compliant foreground color for a given background.\n  - **`IsCompliant`**: Check if a given foreground and background pair meets the required WCAG ratio.\n  - **`GetContrastColorForText`**: Choose a compliant text color based on text size and weight (large/bold rules).\n  - **`IsTextCompliant`**: Verify whether a text foreground/background pair meets the WCAG text contrast criteria.\n  - **`GetContrastColorForUIElement`**: Obtain a compliant color for non-text UI elements, meeting the 3:1 contrast guideline.\n  - **`IsUIElementCompliant`**: Check if a UI element's foreground meets the WCAG non-text contrast ratio.\n  - **`GenerateAccessibleRamp`**: Produces a series of accessible colors from a single base color.\n\n## Getting Started\n\n### Installation\n\n```bash\ndotnet add package AccessibleColors\n```\n\n### Single Contrast Colors\n\n```csharp\nusing AccessibleColors;\nusing System.Drawing;\n\nvar background = Color.FromArgb(255, 0, 0); // Bright red background\nColor foreground = background.GetContrastColor(); // Instantly get a compliant foreground\n\nbool isAccessible = WcagContrastColor.IsCompliant(background, foreground);\n```\n\n### Handling Different Text Sizes/Weights\n\n```csharp\nvar background = Color.FromArgb(32, 32, 32); \ndouble textSizePt = 18.0; // Large text threshold\nbool isBold = false;\n\nColor textForeground = background.GetContrastColorForText(textSizePt, isBold);\nbool isTextCompliant = WcagContrastColor.IsTextCompliant(background, textForeground, textSizePt, isBold);\n```\n\n### UI Elements (Non-Text) Contrast\n\n```csharp\nvar uiBackground = Color.FromArgb(240, 240, 240);\nColor uiElementColor = uiBackground.GetContrastColorForUIElement(); // 3:1 ratio by default\nbool isUIElementAccessible = WcagContrastColor.IsUIElementCompliant(uiBackground, uiElementColor);\n```\n\n### Generate Accessible Color Ramps\n\n```csharp\n// Example: Generating a 5-step ramp for a dark mode UI.\n// When darkMode = true, the ramp is optimized for accessibility against a dark background.\n// By default, the ramp generator considers Color.FromArgb(32, 32, 32) as the reference dark background.\n\nColor baseAccent = Color.FromArgb(0, 120, 215);\nint steps = 5;\nbool darkMode = true;\n\nIReadOnlyList\u003cColor\u003e ramp = ColorRampGenerator.GenerateAccessibleRamp(baseAccent, steps, darkMode);\n\n// Verify compliance against the intended dark background:\nColor darkBackground = Color.FromArgb(32, 32, 32);\nforeach (var c in ramp)\n{\n    bool isCompliant = WcagContrastColor.IsCompliant(darkBackground, c);\n    Console.WriteLine($\"Ramp color: {c}, Compliant (dark bg): {isCompliant}\");\n}\n\n// Use these ramp colors for various states in your UI:\nmyButton.NormalColor = ramp[0];\nmyButton.HoverColor = ramp[1];\nmyButton.PressedColor = ramp[2];\nmyButton.FocusColor = ramp[3];\nmyButton.DisabledColor = ramp[4];\n```\n\n**Important Note About Ramps**:  \nIf your base color is already compliant, the ramp may show little variation since no adjustments are needed. To see a noticeable gradient, start from a non-compliant color (e.g., a light gray on white). The ramp generator will then adjust each step to ensure accessibility, creating a visually distinct gradient.\n\nFor example:\n\n```csharp\nvar startColor = Color.FromArgb(170,170,170); // Non-compliant on white\nvar rampColors = ColorRampGenerator.GenerateAccessibleRamp(startColor, 5, darkMode: false);\nforeach (var c in rampColors)\n{\n    Console.WriteLine($\"Ramp color: {c}, Compliant: {WcagContrastColor.IsCompliant(Color.White, c)}\");\n}\n```\n\nHere, you'll see a clear transition from lighter to darker, all accessible against white.\n\n## Example\n\n```csharp\nusing AccessibleColors;\nusing System.Drawing;\n\n// Single Contrast Example:\nvar bg = Color.FromArgb(128,128,128); // Mid-gray background\nvar fg = bg.GetContrastColor();\nConsole.WriteLine($\"Foreground: {fg} - Compliant: {WcagContrastColor.IsCompliant(bg, fg)}\");\n\n// Text Compliance Example (18pt large text on dark bg):\nvar textBg = Color.FromArgb(32,32,32);\ndouble textSize = 18.0;\nbool bold = true;\nvar textFg = textBg.GetContrastColorForText(textSize, bold);\nConsole.WriteLine($\"Text Foreground: {textFg}, Compliant: {WcagContrastColor.IsTextCompliant(textBg, textFg, textSize, bold)}\");\n\n// UI Element Compliance Example:\nvar uiBg = Color.FromArgb(240, 240, 240);\nvar uiElementColor = uiBg.GetContrastColorForUIElement();\nConsole.WriteLine($\"UI Element Foreground: {uiElementColor}, Compliant: {WcagContrastColor.IsUIElementCompliant(uiBg, uiElementColor)}\");\n\n// Ramp Example (compliance-driven adjustments with dark mode):\n// Here we generate a ramp for a dark background scenario.\n// The ramp will be tuned to be accessible on a dark background (e.g., Color.FromArgb(32, 32, 32)).\nColor baseAccent = Color.FromArgb(0, 120, 215);\nint steps = 5;\nbool darkMode = true;\n\n// This creates a 5-step ramp accessible against a dark background.\nIReadOnlyList\u003cColor\u003e ramp = ColorRampGenerator.GenerateAccessibleRamp(baseAccent, steps, darkMode);\n\n// Since darkMode = true, the intended background is a dark color:\nColor darkBackground = Color.FromArgb(32, 32, 32);\n\nforeach (var c in ramp)\n{\n    bool isCompliant = WcagContrastColor.IsCompliant(darkBackground, c);\n    Console.WriteLine($\"Ramp color: {c}, Compliant with dark background: {isCompliant}\");\n}\n```\n\n## Why This Matters\n\nAccessibility is a cornerstone of inclusive design. Ensuring that text, icons, focus indicators, and other UI elements are distinguishable to everyone improves overall usability. **AccessibleColors** makes it simple to maintain WCAG compliance across your entire UI-no guesswork required.\n\n## Contributing\n\nContributions are welcome! Please open issues, suggest features, or submit pull requests to help improve this library.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](https://github.com/willibrandon/AccessibleColors/blob/main/LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillibrandon%2Faccessiblecolors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillibrandon%2Faccessiblecolors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillibrandon%2Faccessiblecolors/lists"}