目錄
HTML 顏色選擇器
建立顏色選擇器的原始碼
Conclusion
首頁 web前端 html教學 HTML 顏色選擇器

HTML 顏色選擇器

Sep 04, 2024 pm 04:35 PM
html html5 HTML Tutorial HTML Properties HTML tags

眾所周知,HTML被稱為超文本標記語言,它用於在瀏覽器上顯示文本,並藉助其特殊的輔助腳本(例如JavaScript和CSS),使您的內容變得美觀。顏色編碼是美化 HTML 網頁的一部分。

HTML 中的顏色代碼充當標識符,用於在網路上標識和表示該顏色。常用的顏色編碼是十六進制,表示該顏色的“十六進位”代碼。同樣,還有其他顏色代碼,例如 RGB,“紅、綠、藍”的縮寫。另一種顏色代碼稱為 HSL,是「色調、飽和度、亮度」的縮寫。在選擇您喜歡的顏色時,HSL 是一個額外的優勢。

由於一般情況下優先使用十六進位代碼,因此我們已經盡力解釋了十六進位代碼。十六進位顏色代碼包含一個符號、一個雜湊值 (#) 和一組六位數字或數字。它們採用十六進位數字系統,因此「FF」是最高數字,代表十六進位數字系統中的255」。

這六位數字包含三對代表 RB 顏色代碼。在這六位數字中,第一對兩位數字代表「紅色」顏色的強度。因此,第一對位置的“FF”將代表最大強度的紅色。 「00」用於最低強度,「FF」用於最高強度。為了獲得“綠色”顏色,中間的一對代表強度。

同樣,對於“藍色”,最後一對代表強度。

  • 因此,諸如 #FF0000 之類的十六進位數字將導致  HTML 顏色選擇器
  • 十六進位數字,例如 #00FF00 會導致    HTML 顏色選擇器
  • 十六進位數字,例如 #0000FF 將導致  HTML 顏色選擇器
  • 要獲得黃色(「紅色」和「綠色」的組合),會創建一個類似的十六進位數字,例如#FFFF00。

HTML 顏色選擇器

顏色選擇器建立後,讓使用者挑選」自己選擇的顏色。最標準的顏色選擇器用於 Windows 應用程序,例如 MS Word 或 Paint 等。大家都熟悉顏色選擇器;你可以透過看下面的圖片來喚起你的記憶:

HTML 顏色選擇器

輸入類型「顏色用於建立包含顏色的輸入欄位。但某些瀏覽器(例如​​ Internet Explorer 11 及更早版本)不支援此輸入類型。因此,根據瀏覽器的不同,當您使用輸入類型時會彈出一個顏色選擇器。有些瀏覽器會簡單地將此輸入欄位轉換為文字框,如下所示:

HTML 顏色選擇器

因此,當使用支援的瀏覽器時,相同的程式碼將產生以下顏色選擇器調色板。

HTML 顏色選擇器

當點選該彩色框時,會彈出一個調色盤。這裡我使用的是 Google Chrome 版本‘78.0.3904.97’,它支援輸入類型顏色屬性。

HTML 顏色選擇器

建立此類顏色選擇器的程式碼將在下一節中解釋。

建立顏色選擇器的原始碼

以下是在 HTML 中建立最簡單的顏色選擇器的說明。請參閱下面的程式碼:

代碼

<body>
<form action="HTMLColorPicker.html">
Select your favorite color:
<input type="color" name="favcolor" id="color" >
</form>
</body>
登入後複製

上面的 HTML 程式碼包含一個使用名為「color」的輸入類型的 FORM 元素。此顏色輸入類型可建立並顯示最簡單的顏色選擇器,即 Windows 標準顏色選擇器。它允許用戶選擇自己喜歡的顏色。

作為顏色的輸入類型建立一個文字方塊或多個按鈕,其預設背景顏色為「黑色」。當我們點擊它時,它會為用戶顯示顏色選擇。

觀察下面給出的顏色選擇器的工作原理:

第 1 步: 點選預設背景色為「黑色」的按鈕。

HTML 顏色選擇器

上面的程式碼只是創建了一個如上所示的按鈕。

第 2 步: 點選並選擇您的新顏色。

HTML 顏色選擇器

HTML 顏色選擇器

第3步:我們選擇了明亮的綠色來示範。點選“確定”按鈕。

HTML 顏色選擇器

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 exampleI 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:

<body>
<form action="HTMLColorPicker.html">
Select your favorite color:
<input type="color" name="favcolor" id="color" >
<input type="submit" onclick = "ReturnColor()" id="submit" />
</form>
</body>
登入後複製

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.

HTML 顏色選擇器

HTML 顏色選擇器

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.

HTML 顏色選擇器

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1662
14
CakePHP 教程
1418
52
Laravel 教程
1311
25
PHP教程
1261
29
C# 教程
1234
24
HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

See all articles