DEV Community

Cover image for Build a Streamlit App to Find the Closest CSS Color Name Using Python 🎨✨
Nishkarsh Pandey
Nishkarsh Pandey

Posted on

5 2 2 2 2

Build a Streamlit App to Find the Closest CSS Color Name Using Python 🎨✨

Colors are everywhere in web and app development β€” but what if you pick a random color and want to know its closest official CSS color name? Instead of guessing or endlessly searching, let’s build a fun Streamlit app that does this for you in seconds!

Why This App?🎨🎨
Quickly find the closest CSS4 color name for any color you choose.
Visual comparison of your color and the matched CSS color side-by-side.
Great for designers, developers, or anyone curious about colors!

Tools We’ll Use
Streamlit β€” super easy framework to build interactive web apps with Python.
matplotlib β€” provides a comprehensive dictionary of CSS4 color names and their hex codes.
Python standard library β€” for math and color conversions.

How It Works
User picks a color using a color picker (or enters an RGB/hex code).
The app converts the selected color into RGB values normalized between 0 and 1.
We compare this to every CSS4 color in matplotlib.colors.CSS4_COLORS.
Calculate the Euclidean distance between the RGB values to find the closest match.
Display the closest CSS color name, its hex code, and a side-by-side color preview.

The Code

import streamlit as st
import matplotlib.colors as mcolors
from math import sqrt
def hex_to_rgb(hex_color):
    return mcolors.to_rgb(hex_color)
def rgb_distance(rgb1, rgb2):
    return sqrt(sum((a - b) ** 2 for a, b in zip(rgb1, rgb2)))
def closest_css_color(target_rgb):
    css4_colors = mcolors.CSS4_COLORS
    closest_name = None
    min_dist = float('inf')
    for name, hex_val in css4_colors.items():
        rgb = hex_to_rgb(hex_val)
        dist = rgb_distance(rgb, target_rgb)
        if dist < min_dist:
            min_dist = dist
            closest_name = name
    return closest_name, css4_colors[closest_name]
st.set_page_config(page_title="Closest Color Name Finder 🎨")
st.title("🎨 Closest CSS Color Name Finder")
st.markdown("Pick a color and we'll tell you the closest CSS4 color name!")
hex_input = st.color_picker("Pick a color:", "#6495ED")
rgb_input = mcolors.to_rgb(hex_input)
rgb_255 = tuple(int(c * 255) for c in rgb_input)
st.write(f"RGB: {rgb_255}")
closest_name, closest_hex = closest_css_color(rgb_input)
st.subheader("πŸ” Closest CSS Color Name")
st.write(f"**{closest_name}** (`{closest_hex}`)")
st.markdown("### 🎨 Color Comparison")
col1, col2 = st.columns(2)
with col1:
    st.markdown("**Your Color**")
    st.markdown(f"<div style='background-color:{hex_input};height:100px;border-radius:10px;'></div>", unsafe_allow_html=True)
with col2:
    st.markdown("**Closest Named Color**")
    st.markdown(f"<div style='background-color:{closest_hex};height:100px;border-radius:10px;'></div>", unsafe_allow_html=True)
Enter fullscreen mode Exit fullscreen mode

Output :

Output_image

How to Run the App
Make sure you have Python installed.
Install Streamlit and matplotlib if you don’t have them:

pip install streamlit matplotlib

Enter fullscreen mode Exit fullscreen mode

Save the above code as color_name_finder.py
Run it with:

streamlit run color_name_finder.py
Enter fullscreen mode Exit fullscreen mode

What Next?
Add image upload to extract dominant colors.
Show top 5 closest CSS colors instead of just one.
Include color harmony suggestions based on the chosen color.

This app is a great starter project to learn Streamlit and get familiar with color manipulation in Python β€” give it a try and play with colors!

If you enjoyed this, please leave a reaction or comment! Happy coding! πŸš€

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (4)

Collapse
 
madhurima_rawat profile image
Madhurima Rawat β€’

Your projects are really impressive!
If you deploy them using Streamlit Cloud, you'll likely get more engagement 🌐 since users can try them out without any setup.

Streamlit deployment is easy too πŸ’» I’ve deployed many of my own projects using it.

Looking forward to seeing more amazing work from you! πŸš€

Collapse
 
nish2005karsh profile image
Nishkarsh Pandey β€’

Thank you so much! 😊
I really appreciate the kind words β€” and yes, you're absolutely right about Streamlit Cloud! I’ve been meaning to deploy a few of my projects there, and your comment just gave me the nudge I needed.
Would love to check out some of your deployed projects too β€” always great to learn from fellow builders! πŸš€

Collapse
 
madhurima_rawat profile image
Madhurima Rawat β€’

I’m happy to know that my comment encouraged you to give deployment a try! πŸ’‘

If you want to check out my projects, visit:
share.streamlit.io/user/madhurimar...
This page shows all my projects. 🌐

For my machine learning project πŸ€– specifically, check out:
ml-model-datasets-using-apps-3gy37...

Can’t wait to see your projects too! ✨

Thread Thread
 
nish2005karsh profile image
Nishkarsh Pandey β€’

Thanks so much for sharing your projects! I’m really inspired. I’ll be uploading my projects soonβ€”just putting some final touches on them. Looking forward to sharing and learning together! 😊✨

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

πŸ‘‹ Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s dayβ€”leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay