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)
Output :
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
Save the above code as color_name_finder.py
Run it with:
streamlit run color_name_finder.py
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! π
Top comments (4)
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! π
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! π
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! β¨
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! πβ¨