Creating a Color Picker is a fun and practical way to explore HTML, CSS, and JavaScript while building an interactive web tool. In this tutorial I will break down the structure and styling behind the Color Picker, including detailed comments on HTML, CSS, and JavaScript.
A live version of the app is available here.
The HTML file for this color picker defines a structured layout to hold all the essential elements like the color display box, color hexagon grid, sliders, and color mode toggles. Here’s a breakdown of the relevant code:
<h2>
tag: A header titled “Color Picker” is displayed at the top.<div id="hexagonPicker">
: This div
holds the hexagon grid of colors. The .hexagon-picker
class will style it as a grid.<div id="displayBox">
: This box displays the currently selected color, providing users with visual feedback.<div id="colorBox">
: A secondary box, styled similarly to displayBox
, acts as a color preview for other parts of the interface.The CSS is crucial in transforming these HTML elements into a visually engaging and user-friendly color picker. Here’s the CSS code with explanations.
The hexagonal grid structure is managed by CSS using a flexible container layout and a hexagonal clipping path for each color cell.
.hexagon-picker
: Defines the layout for the hexagonal picker grid. The display: flex
and flex-wrap: wrap
settings create a flexible grid.width
property limits the picker’s size, while margin: auto
centers it.Each color cell in the grid is shaped into a hexagon using the clip-path
property.
.hex
class: Defines each individual hexagonal cell.width
and height
create a basic rectangle that fits into a hexagonal shape.clip-path
: Shapes the element into a hexagon, creating the distinctive color cells.cursor: pointer
gives visual feedback that the element is interactive.The display box shows the color users have selected and is styled to appear like a swatch.
.color-box
: Used to display the currently selected color.JavaScript brings the picker to life, allowing users to interact with the color cells and view their selected colors.
getComputedStyle
and updates the background color of both displayBox
and colorBox
.The JavaScript code also allows the picker to handle different color models: HEX, RGB, and HSL.
hexToRgb
Function: Converts a HEX color code to an RGB format, which is useful when displaying or manipulating colors.parseInt
function extracts red, green, and blue values from the HEX code.To enhance usability, a color mode toggle is implemented. This mode allows users to switch between color and grayscale modes.
toggleColorMode
Function: This function (defined elsewhere) updates the colors displayed in the picker based on the mode selected.The combination of structured HTML, detailed CSS for layout and styling, and JavaScript to handle interactions, creates a highly interactive color picker. This tool allows users to explore and select colors intuitively.
In this section, we’ve covered: - The layout for the color picker in HTML, including elements for color selection and display. - The CSS that styles these elements, including the hexagonal grid structure and the color display box. - JavaScript code to handle color selection, updating both the display and mode switch options.
The Color Picker application uses flexible web design techniques to build an interactive, visually appealing tool that can enhance any web-based design process. Let’s continue to explore the palette generation in the next part.
The palette section of the Color Picker enhances the functionality by allowing users to generate color palettes based on their chosen color. This feature is useful for designers and developers who need coordinated color schemes for their projects. Here’s a detailed breakdown of how the palette functionality works in HTML, CSS, and JavaScript.
The HTML defines two palette types: Analogous and Complementary. Each palette section contains a button to generate the colors and a container to display the palette once generated.
<button>
: Each palette has a button to initiate the color generation process. The buttons have unique IDs (paletteAnalogousBtn
and paletteComplementaryBtn
), allowing the JavaScript to target them for functionality.<div id="paletteAnalogous">
and <div id="paletteComplementary">
: These containers hold the generated colors for each palette type. Initially hidden, they become visible after clicking the “Generate” button.The CSS styles each color in the palette as a square box. This layout makes it easy to see how the colors in the palette relate to each other visually.
.color-square
: Defines each color in the palette. The fixed width and height ensure uniformity in size.cursor: pointer
property gives visual feedback that the color squares are clickable.The CSS also adjusts the size of each color square on smaller screens. This ensures that the palette remains usable and visually pleasing on devices of all sizes.
On screens smaller than 768px, each square is reduced to 40px by 40px to fit more comfortably on mobile displays.
JavaScript handles the color generation when users click the “Generate” buttons. Each palette is created based on the currently selected color, with two distinct generation functions for analogous and complementary colors.
The analogous palette consists of colors that are close to each other on the color wheel. JavaScript generates these colors by shifting the hue value slightly.
generateAnalogousPalette
Function: Creates an array of colors by adjusting the hue of the selected color. The hue is shifted in increments of 30 degrees to obtain similar colors.displayPalette
Function: Takes the generated colors and displays them in the paletteAnalogous
container.The complementary palette uses colors that are directly opposite on the color wheel, providing high contrast.
generateComplementaryPalette
Function: Creates an array with the original color and its complementary hue by shifting the hue by 180 degrees.displayPalette
Function: Similar to the analogous palette, this function displays the complementary colors.To adjust the hue of a color, a helper function is used that modifies the color properties based on HSL values.
adjustHue
Function: This function takes a color and degree adjustment, converts it to HSL, modifies the hue, and then converts it back to RGB.rgbToHsl
and hslToRgb
help convert colors between different models, enabling precise hue adjustments.The displayPalette
function renders the generated colors in their respective containers.
div
is created for each color, assigned the .color-square
class, and styled with the respective color.flex
styling.The palette feature in this Color Picker application makes it more than just a color selection tool. It helps users create harmonious or contrasting color schemes for design work. This section covered: - The HTML setup with buttons and containers for the analogous and complementary palettes. - CSS styling for the color squares, ensuring a uniform and responsive design. - JavaScript functions for generating and displaying both analogous and complementary palettes.
This detailed implementation provides a robust, interactive tool for users to explore color relationships and enhance their design workflows.
The final results on iOS:
and on desktop: