ometimes, deciding on a color scheme for a site is more of a problem than is implementing it. I’ve sat on committees intended to design Web sites by consensus and I can tell you from experience: Getting more than two people to settle on a color scheme is no easy task! A little programming helps though. You can simplify color scheme selection by letting people see how their color preferences interact on-screen.
Although displaying a Web-safe color palette is relatively easy, simply displaying the colors doesn’t give users an easy way to see how selected colors interact with each other. How can you get people to choose and agree on sensible colors for a site?
Make the Web-safe color display interactive, using JavaScript to apply selected colors to some sample content and let users see immediately how their chosen colors look together. Figure 1 shows how the completed application looks.
Create a Web-safe Palette This solution has two goals?displaying the Web-safe palette and making it interactive. Displaying the palette is easy; you use some nested loops and a simple array to build a table whose cells each represent a different entry in the palette. A pair of radio buttons on the page lets users choose whether they’re selecting a foreground or a background color. Finally, to make the display interactive, you can connect the onClick event for each colored cell in the table to a function that applies the selected color to either the foreground or background color of a separate display?based on the currently selected radio button. Simple event handlers for the radio buttons maintain the foreground/background value, and a “dummy” tag holds content so users can see the effect of their chosen colors immediately. To get started, open up your favorite text editor and add the following code:
The CSS Rules shown above position elements on the page. Next, create three tags to hold the color table, the radio button controls, and a test area where the color effects appear. Because each of the cells will be clickable, the td rule turns the standard cursor into the hand cursor when users move the mouse over a table cell. Note that the rules don’t specify any colors?users will determine the colors in the testArea by clicking on the cells in the palette. | Add the following between the tags:
The colorTable ‘s content will be generated by a function called createTable(). Also note that the controls possesses two radio buttons?one to set the foreground color and one to set the background color. Each radio button has an onClick event that sets the value of a flag?colorToSet?in the script to either “fore” or “back”. The function that displays the selected colors in the testArea will check this flag to see which property to change. The controls also contains labels that display the current color values of the foreground and background properties of the testArea. Initially, these values are set to black text (#000000) on a white (#ffffff) background. Finally, the testArea contains some placeholder text so users can see how the colors interact. Make the Palette Interactive The preceding code creates a table by using three nested for loops. The colors array holds the hexadecimal color values acceptable for each of the red, green, and blue components of a Web-safe color palette. The code then uses the nested loops to iterate through each of the values. The i loop represents the red component, the j loop represents the green component, and the k loop represents the blue component of the color to be displayed. By including the HTML table markup tags and looping through the acceptable values of the Web-safe palette stored in the colors array, the createTable() function creates a table with 6 colors and 36 rows?the 216 colors of the Web-safe color palette. You may have noticed that as the code constructs the tags, it also adds an onClick event handler that points to a function called setColor(). The setColor() function takes the | element itself as an argument (the "this" argument). You'll need to add the setColor() function?and the colorToSetFlag mentioned earlier to the page. Add the following to the script: |
When the page first loads, the colorToTest flag is set to 'fore', but the flag value changes from 'fore' to 'back' as users change the selected radio button. The setColor() function uses DOM properties to access the text stored in the element passed to the function. It then uses that text (the color value) to apply the selected color to either the foreground or background of the testArea | depending on the current colorToSet flag value. Finally, the setColor() function sets the innerHTML property of either the foreground or background labels to the selected color value. That's all there is to it! You can use this Web-safe color palette application to help select a color scheme for anything from a single element to an entire site. You could easily extend the application to let users change the font, weight, and size of the text in the test area. In fact, the closer you make the content of the test area to the actual content of the intended site, the more likely it is that the process of selecting a color scheme will go smoothly. About Our Editorial ProcessAt DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere. See our full editorial policy. About Our JournalistCharlie Frank Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience. View Author Global stock stability amid rising US interest rates Rashan Dixon April 18, 2024 Rivos Inc. raises $250 million pre-launch for AI chips Noah Nguyen April 18, 2024 Goldman Sachs advises caution in predicting Bitcoin prices Cameron Wiggins April 18, 2024 Predicted rise in social security benefits amid inflation Noah Nguyen April 18, 2024 Gold prices dip amid assertive fiscal comments Johannah Lopez April 18, 2024 Vorlon raises $15.7M to strengthen API security Noah Nguyen April 18, 2024 Global stock stability amid rising US interest rates Rashan Dixon April 18, 2024 1:44 PM Rivos Inc. raises $250 million pre-launch for AI chips Noah Nguyen April 18, 2024 1:19 PM Goldman Sachs advises caution in predicting Bitcoin prices Cameron Wiggins April 18, 2024 1:14 PM Predicted rise in social security benefits amid inflation Noah Nguyen April 18, 2024 1:09 PM Gold prices dip amid assertive fiscal comments Johannah Lopez April 18, 2024 11:46 AM Vorlon raises $15.7M to strengthen API security Noah Nguyen April 18, 2024 11:28 AM Last April social security disbursement approaching Johannah Lopez April 18, 2024 11:16 AM Rippling to pursue substantial funding round Johannah Lopez April 18, 2024 8:33 AM Simpson’s lawyer battles to protect estate from Goldmans Noah Nguyen April 18, 2024 8:31 AM April’s Social Security retirement payments underway Cameron Wiggins April 17, 2024 5:54 PM Rivian develops software to rank EV chargers Rashan Dixon April 17, 2024 5:50 PM Simpson’s will leaves Goldman family uncompensated April Isaacs April 17, 2024 5:26 PM Framework grapples with software sustainability issues Noah Nguyen April 17, 2024 3:44 PM Surprising economic growth attributed to China’s robust exports and domestic demand Rashan Dixon April 17, 2024 3:04 PM COBOL at 65: still a powerhouse in the tech industry Cameron Wiggins April 17, 2024 1:59 PM California’s minimum wage hike worries restaurateurs Noah Nguyen April 17, 2024 1:05 PM IRS introduces user-friendly, free tax software ‘Direct File’ Johannah Lopez April 17, 2024 11:10 AM Social Security COLA rise expects to strain retirees’ finances Rashan Dixon April 17, 2024 7:11 AM Rising living costs force seniors back to work Rashan Dixon April 16, 2024 5:28 PM Gershanok leaves McKinsey, discusses workplace challenges April Isaacs April 16, 2024 3:31 PM India’s economy hangs on election outcome Noah Nguyen April 16, 2024 3:28 PM Tech-Infused Real Estate: The Future of Property Management DevX-Admin April 16, 2024 1:51 PM Samsung disrupts Apple’s dominance in smartphone market Noah Nguyen April 16, 2024 11:52 AM Sony’s XR beats Apple’s Vision Pro in performance Noah Nguyen April 16, 2024 11:42 AM Upcoming Social Security payments begin April 17th Noah Nguyen April 16, 2024 8:34 AM |