

To do that, we need to “draw” the image onto a element and then use the canvas context ( ctx) getImageData() method to produce a list of the image’s colors.Menus Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Hover Tabs Top Navigation Responsive Topnav Split Navigation Navbar with Icons Search Menu Search Bar Fixed Sidebar Side Navigation Responsive Sidebar Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Sidebar with Icons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Link Equal Width Menu Links Fixed Menu Slide Down Bar on Scroll Hide Navbar on Scroll Shrink Navbar on Scroll Sticky Navbar Navbar on Image Hover Dropdowns Click Dropdowns Cascading Dropdown Dropdown in Topnav Dropdown in Sidenav Resp Navbar Dropdown Subnavigation Menu Dropup Mega Menu Mobile Menu Curtain Menu Collapsed Sidebar Collapsed Sidepanel Pagination Breadcrumbs Button Group Vertical Button Group Sticky Social Bar Pill Navigation Responsive Header Let’s get started! Step 1: Read image colors from the canvasĬanvas lets us “read” the colors contained in an image. And these won’t just be random guesses - we’ll use binary search techniques to make this process quick.

Finally, we’ll adjust the opacity of our overlay until the text contrast hits the readability goal.Next, we’ll prepare a color-mixing formula we can use to test different opacity levels on top of that pixel’s color.We’ll find the pixel in the image that has the least contrast with the text.We’ll put the image in an HTML, which will let us read the colors of each pixel in the image.To find the optimal overlay opacity we’ll go through four steps: Our final result will be a value we can apply to the CSS opacity property of the overlay that gives us the right amount of transparency that makes the text 4.5 times lighter than the background.

To complicate things a bit, we’ll use an image with both dark and light space and make sure the overlay takes that into account. Given those inputs, we want to find the overlay opacity level that makes the text readable without hiding the image so much that it, too, is difficult to see. Let’s pick a text color, a background image, and an overlay color as a starting point. We’ve said we want readable text on top of a background image, but what does “readable” even mean? For our purposes, we’ll use the WCAG definition of AA-level readability, which says text and background colors need enough contrast between them such that that one color is 4.5 times lighter than the other. Here’s the planįirst, let’s get specific about our goals. But where’s the fun in that? What I want to show you is how this tool works so you have a new way to handle this all-too-common problem. We could say “Problem solved!” and simply end this article here.
