Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
If you want to check the website for compliance with WCAG guidelines programmatically, the Aspose.HTML for Java provides the com.aspose.html.accessibility package, which is for all Web Accessibility related manipulations and checks.
This article will discuss color and proper contrast according to WCAG, the world’s authority on web accessibility. You learn how to check color contrast using Aspose.HTML for Java API and make your web content easy to read for all users.
Ensuring sufficient color contrast in your web content is essential for creating accessible and user-friendly websites. Poor color contrast can make content difficult to read, especially for users with visual impairments. The Web Content Accessibility Guidelines (WCAG) define minimum contrast ratios to ensure that text is readable by all users. The test criteria for color contrast are contained in the guideline 1.4 – Distinguishable.
For color contrast, WCAG defines two levels of success criteria: AA (minimal contrast) and AAA (enhanced contrast). These criteria checks the contrast ratio between text and its background, which ranges from 1:1 to 21:1.
Below you can see an example of proper contrast between font and background and an example of poor contrast:
Let’s write the HTML code for this example to check-color.html file and check color contrast using Aspose.HTML for Java API.
1<html>
2 <head>
3 <style>
4 div {
5 font-family: sans-serif;
6 text-align: center;
7 font-weight: bold;
8 text-align: center;
9 padding: 10px;
10 border-radius: 15px;
11 width: 300px;
12 margin: auto;
13 }
14 .bad {
15 background-color: #8b030e;
16 font-size: 16px;
17 }
18 .good {
19 background-color: #fff0f2;
20 font-size: 18px;
21 }
22 </style>
23 </head>
24 <body>
25 <div class="good">Text with good contrast</div>
26 <div class="bad">Text with bad contrast</div>
27 </body>
28</html>For visually impaired users, it is necessary to be able to perceive the content on the page normally. The contrast between text and background should be sufficient for its perception. To check the criteria related to contrast checking, find the principle in the code and pass it to the accessibility validator. In the following example, the validator is created for two criteria: 1.4.3 and 1.4.6 from the 1.4 Guideline:
1// Validate HTML accessibility for color contrast in Java using WCAG criteria
2
3// Prepare a path to a source HTML file
4String documentPath = "check-color.html";
5
6// Initialize a webAccessibility container
7WebAccessibility webAccessibility = new WebAccessibility();
8
9// Get Principle "1.Perceivable" by code "1" and get guideline "1.4"
10Guideline guideline = webAccessibility.getRules()
11 .getPrinciple("1").getGuideline("1.4");
12
13// Get criterion by code, for example 1.4.3
14Criterion criterion143 = guideline.getCriterion("1.4.3");
15
16// Get criterion by code, for example 1.4.6
17Criterion criterion146 = guideline.getCriterion("1.4.6");
18
19// Create an accessibility validator, pass the found guideline
20// as parameters and specify the full validation settings
21List<IRule> rules = new List<>();
22rules.add(criterion143);
23rules.add(criterion146);
24
25AccessibilityValidator validator = webAccessibility.createValidator(
26 rules,
27 ValidationBuilder.getAll()
28);
29
30final HTMLDocument document = new HTMLDocument(documentPath);
31ValidationResult validationResult = validator.validate(document);
32if (!validationResult.getSuccess()) {
33 System.out.println(validationResult.saveToString());
34}This example demonstrates how to perform a web accessibility check of an HTML document against several WCAG criteria at once — in this case, 1.4.3 (text contrast) and 1.4.6 (enhanced contrast). First, a list of these criteria is created, then validation is run using AccessibilityValidator, and the final result is output as a full-text report. This approach is convenient for quickly assessing a document’s compliance with several rules without in-depth analysis.
The following Java code performs a more detailed check for a single criterion (1.4.3). It does not simply validate the document but extracts specific errors related to HTML elements and outputs information about the violated rules, error text, and problematic areas of the HTML code. This method is suitable for debugging and pinpointing places where accessibility is violated.
1// Check color contrast on an HTML document using Java
2
3// Prepare a path to a source HTML file
4String documentPath = "check-color.html";
5
6// Initialize a webAccessibility container
7WebAccessibility webAccessibility = new WebAccessibility();
8
9// Get Principle "1.Perceivable" by code "1" and get guideline "1.4"
10Guideline guideline = webAccessibility.getRules()
11 .getPrinciple("1").getGuideline("1.4");
12
13// Get criterion by code, for example 1.4.3
14Criterion criterion = guideline.getCriterion("1.4.3");
15
16// Create an accessibility validator, pass the found guideline
17// as parameters and specify the full validation settings
18AccessibilityValidator validator = webAccessibility.createValidator(
19 criterion,
20 ValidationBuilder.getAll()
21);
22
23final HTMLDocument document = new HTMLDocument(documentPath);
24ValidationResult validationResult = validator.validate(document);
25if (!validationResult.getSuccess()) {
26 // Get all result details
27 for (RuleValidationResult ruleResult : validationResult.getDetails()) {
28 // If the result of the rule is unsuccessful
29 if (!ruleResult.getSuccess()) {
30 // Get errors list
31 for (ITechniqueResult result : ruleResult.getErrors()) {
32 // Check the type of the erroneous element, in this case
33 // we have an error in the html element rule
34 if (result.getError().getTarget().getTargetType() == TargetTypes.HTMLElement) {
35 // Element of file with error
36 HTMLElement rule = (HTMLElement) result.getError().getTarget().getItem();
37
38 System.out.println(String.format("Error in rule %s : %s",
39 result.getRule().getCode(), result.getError().getErrorMessage()));
40
41 System.out.println(String.format("CSS Rule: %s",
42 rule.getOuterHTML()));
43 }
44 }
45 }
46 }
47}criterion variable now holds the specific criterion “1.4.3,” and you can use it when creating a validator to check your web content against this particular criterion.criterion and ValidationBuilder.getAll() to it as parameters.criterion – is the specific accessibility criterion that represents the specific rule or requirement you want to validate against.ValidationBuilder.getAll() – is the parameter that means you want to check for all aspects covered by the criterion.document) method to check the HTML content against the selected criterion. The result is stored in the validationResult variable.When validation fails, the output might look like this:
1Error in rule G18 : Make sure the contrast ratio between the text (and images of text) and the background behind the text is at least 4.5:1 for text less than 18 points if it is not in bold,
2and less than 14 points if it is in bold.
3CSS Rule: <div class="bad">Text with bad contrast</div>See Also
Use the free online Color Contrast Checker to check the contrast ratios in your web designs. This tool provides feedback on whether your color choices meet accessibility standards.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.