您的位置:

优雅地使用颜色:Python实现色彩名称数据库

一、介绍

在设计和开发应用程序时,颜色是有用的视觉元素之一。 人们经常使用颜色来表达信息,吸引用户的注意力或增强设计的吸引力。 但是,当需要使用颜色时,很难记住色彩名称或十六进制码。本文通过Python实现色彩名称数据库,通过简单的字符串调用快速识别常用颜色名称和它们对应的十六进制码。

二、背景

在开发过程中,很多颜色都以十六进制码的方式进行定义和使用。十六进制码只是HTML和CSS中使用的一种表示颜色的方式。当设计中的使用颜色数量增加时,RGB颜色空间和十六进制码则逐渐变得不便于记忆,因此颜色名称通常更为常用。

三、实现

本文通过Python实现色彩名称数据库。主要通过如下三方面实现:

1. 颜色名称对应表

实现方案是首先创建一个包含常用颜色名称和相应十六进制码的数据库。

color_values = {"aliceblue": "#f0f8ff", "antiquewhite": "#faebd7", "aqua": "#00ffff", 
                 "aquamarine": "#7fffd4", "azure": "#f0ffff", "beige": "#f5f5dc",
                 "bisque": "#ffe4c4", "black": "#000000", "blanchedalmond": "#ffebcd", 
                 "blue": "#0000ff", "blueviolet": "#8a2be2", "brown": "#a52a2a",
                 "burlywood": "#deb887", "cadetblue": "#5f9ea0", "chartreuse": "#7fff00",
                 "chocolate": "#d2691e", "coral": "#ff7f50", "cornflowerblue": "#6495ed",
                 "cornsilk": "#fff8dc", "crimson": "#dc143c", "cyan": "#00ffff",
                 "darkblue": "#00008b", "darkcyan": "#008b8b"}

2. 获取对应值

创建了数据库后,可以通过简单的字符串调用,获取颜色对应的十六进制码。例如:

# 获取颜色 aliceblue 的十六进制码
color_values["aliceblue"]    # 输出 "#f0f8ff"

3. 判断是否在数据库中

在使用过程中,还需要判断颜色字符串是否在数据库中。一种方法是通过枚举keys,然后比较。不过,使用in关键字明显更高效,代码如下:

if color in color_values:
    print('hex code for', color, 'is', color_values[color])
else:
    print('Color not found in database')

四、应用场景

在开发任何需要颜色的应用时,实现颜色数据库可以是很有用的。例如,在可视化或数据可视化应用程序中,颜色的数量通常非常大,开发者需要频繁调用颜色名称或对应的十六进制码来控制颜色输出。通过实现颜色名称数据库可以让代码更加可读和易于维护。

五、总结

通过下面的Python代码实现了颜色名称对应的十六进制码的简单查询,可以大大方便调用颜色值来实现各种效果。

color_values = {"aliceblue": "#f0f8ff", "antiquewhite": "#faebd7", "aqua": "#00ffff", 
                 "aquamarine": "#7fffd4", "azure": "#f0ffff", "beige": "#f5f5dc",
                 "bisque": "#ffe4c4", "black": "#000000", "blanchedalmond": "#ffebcd", 
                 "blue": "#0000ff", "blueviolet": "#8a2be2", "brown": "#a52a2a",
                 "burlywood": "#deb887", "cadetblue": "#5f9ea0", "chartreuse": "#7fff00",
                 "chocolate": "#d2691e", "coral": "#ff7f50", "cornflowerblue": "#6495ed",
                 "cornsilk": "#fff8dc", "crimson": "#dc143c", "cyan": "#00ffff",
                 "darkblue": "#00008b", "darkcyan": "#008b8b"}

# 获取颜色 aliceblue 的十六进制码
print(color_values["aliceblue"])    # 输出 "#f0f8ff"

# 判断是否在数据库中
color = 'red'
if color in color_values:
    print('hex code for', color, 'is', color_values[color])
else:
     print('Color not found in database')
优雅地使用颜色:Python实现色彩名称数据库

2023-05-12
优雅的使用CSS #XXXXXX颜色代码 - 提升网站色彩表

2023-05-12
如何优雅地使用CSS颜色代码

2023-05-12
Python颜色表详解

2023-05-17
如何为python日志输出着色,python改变输出颜色

2022-11-18
Python中的色彩平衡快捷键

2023-05-10
让你的网站颜值翻倍:优雅地运用CSS颜色属性

2023-05-12
python图片转灰色(python灰度图像变彩色)

2022-11-11
用python实现颜色识别功能的简单介绍

2022-11-15
HSV色彩空间:一种更直观的色彩模型

2023-05-23
python画图笔记(python画图作业)

2022-11-10
以华丽的数字字体为你的Python应用添色彩

2023-05-12
探究 Echarts 饼图的颜色色彩设计

2023-05-19
颜色矩原理及python实现的简单介绍

2022-11-21
#FFFFFF色彩代码

2023-05-12
使用CSS hex值优化您的页面色彩

2023-05-12
cad打印个别有颜色,cad打印块颜色显示彩色

2023-01-05
利用Python生成网页元素的漂亮#fff颜色

2023-05-13
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)