众所周知,HTML被称为超文本标记语言,它用于在浏览器上显示文本,并借助其特殊的辅助脚本(例如JavaScript和CSS),使您的内容变得美观。颜色编码是美化 HTML 网页的一部分。
HTML 中的颜色代码充当标识符,用于在网络上标识和表示该颜色。常用的颜色编码是十六进制,表示该颜色的“十六进制”代码。同样,还有其他颜色代码,例如 RGB,“红、绿、蓝”的缩写。另一种颜色代码称为 HSL,是“色调、饱和度、亮度”的缩写。在选择您喜欢的颜色时,HSL 是一个额外的优势。
由于一般情况下优先使用十六进制代码,因此我们已经尽力解释了十六进制代码。十六进制颜色代码包含一个符号、一个哈希值 (#) 和一组六位数字或数字。它们采用十六进制数字系统,因此“FF”是最高数字,代表十六进制数字系统中的“255”。
这六位数字包含三对代表 RB 颜色代码。在这六位数字中,第一对两位数字代表“红色”颜色的强度。因此,第一对位置的“FF”将代表最大强度的红色。 “00”用于最低强度,“FF”用于最高强度。为了获得“绿色”颜色,中间的一对代表强度。
同样,对于“蓝色”,最后一对代表强度。
- 因此,诸如 #FF0000 之类的十六进制数字将导致
- 十六进制数字,例如 #00FF00 将导致
- 十六进制数字,例如 #0000FF 将导致
- 要获得黄色(“红色”和“绿色”的组合),会创建一个类似的十六进制数字,例如#FFFF00。
HTML 颜色选择器
颜色选择器创建后,允许用户“挑选”自己选择的颜色。最标准的颜色选择器用于 Windows 应用程序,例如 MS Word 或 Paint 等。大家都熟悉颜色选择器;你可以通过看下面的图片来唤起你的记忆:
输入类型“color”用于创建包含颜色的输入字段。但某些浏览器(例如 Internet Explorer 11 及更早版本)不支持此输入类型。因此,根据浏览器的不同,当您使用输入类型时会弹出一个颜色选择器。有些浏览器会简单地将此输入字段转换为文本框,如下所示:
因此,当使用支持的浏览器时,相同的代码将产生以下颜色选择器调色板。
单击该彩色框时,会弹出一个调色板。这里我使用的是 Google Chrome 版本‘78.0.3904.97’,它支持输入类型颜色属性。
创建此类颜色选择器的代码将在下一节中解释。
创建颜色选择器的源代码
以下是在 HTML 中创建最简单的颜色选择器的说明。请参阅以下代码:
代码
上面的 HTML 代码包含一个使用名为“color”的输入类型的 FORM 元素。此颜色输入类型创建并显示最简单的颜色选择器,即 Windows 标准颜色选择器。它允许用户选择自己喜欢的颜色。
作为颜色的输入类型创建一个文本框或多个按钮,其默认背景颜色为“黑色”。当我们点击它时,它会为用户显示颜色选择。
观察下面给出的颜色选择器的工作原理:
第 1 步: 单击默认背景色为“黑色”的按钮。
上面的代码只是创建了一个如上所示的按钮。
第 2 步: 单击并选择您的新颜色。
第3步:我们选择了明亮的绿色进行演示。单击“确定”按钮。
In the above screen-shots, you can easily see the selected color is shown in the last screen-shot.
The input type ‘color’ provides this simple functionality of a color picker in HTML5. After picking your color, it is your choice of what the selected color can be used for.
In the following example, I incremented the above example and modified it with some inclusions.
The following example is a combination of HTML and Javascript. This example has a FORM element that uses the input type ‘color’ tag. This FORM, when submitted, our JAVASCRIPT is triggered.
Observe the source code for the FORM element below:
Code:
We added a new line to our previous program. A submit button. This submit button is when clicked; our Java script is triggered, which is given below:
function ReturnColor(c) { //saving the selected color value by ID var c= document.getElementById("color").value; var str= new String ("You chose:"); //The color is saved as its HEX color code. document.write(str+c); }
When the ‘Submit’ button is clicked, our function in javascript is triggered. The above function, ReturnColor (), returns the HEX code, that is, Hexadecimal code for the selected color by our color picker. When the code is executed, the following is our output.
The above output is in the HEX code. The 6 numbers represent the inclusion of Red, Green and Blue colors resulting in the selected color. This HEX code can also be converted easily into RGB code.
Similarly, we can save the above code and set it as a background color or a font color for the user. To do so, we added a few more lines of code into our already existing source code.
Following is the complete code, with the HTML body remaining the same:
<script> function ReturnColor(c) { //saving the selected color value by ID var c= document.getElementById("color").value; var str= new String ("You chose:"); //The color is saved as its HEX color code document.write(str+c); document.write("<br/>"); //A HEX color code can be converted into RGB code var R=c.slice(1,3); var G=c.slice(3,5); var B=c.slice(5); //Displaying the corresponding RGB code document.write("In RGB format, RGB(" + parseInt(R,16) + "," + parseInt(G,16) + "," + parseInt(B,16) + ")"); document.write("<br/>"); //Setting our selected color as Font color var color = c; var str1 = "Your color will appear as this font color"; var str2 = str1.fontcolor(c); document.write(str2); //Setting our selected color as Background color document.write("<div style='border: solid; height: 90px; width: 90px; background-color:"+color+"'/>"); } </script>
This is our complete script. When the code is executed, and a color is selected, the following is the output that is displayed.
Conclusion
There are many ways and many combinations that can help you to create a color picker, that too smart one. For example, with the combination of HTML5 and CSS and JavaScript, you can use yet another element called ‘canvas’ that has its own libraries that helps create a lightweight, small and cross-browser color picker. But that’s for another time.
以上是HTML 颜色选择器的详细内容。更多信息请关注PHP中文网其他相关文章!

HTML、CSS和JavaScript在Web开发中的作用分别是:1.HTML定义网页结构,2.CSS控制网页样式,3.JavaScript添加动态行为。它们共同构建了现代网站的框架、美观和交互性。

HTML的未来充满了无限可能。1)新功能和标准将包括更多的语义化标签和WebComponents的普及。2)网页设计趋势将继续向响应式和无障碍设计发展。3)性能优化将通过响应式图片加载和延迟加载技术提升用户体验。

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增强WebevebDevelopment。

HTML是构建网页结构的基石。1.HTML定义内容结构和语义,使用、、等标签。2.提供语义化标记,如、、等,提升SEO效果。3.通过标签实现用户交互,需注意表单验证。4.使用、等高级元素结合JavaScript实现动态效果。5.常见错误包括标签未闭合和属性值未加引号,需使用验证工具。6.优化策略包括减少HTTP请求、压缩HTML、使用语义化标签等。

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)