สี CSS RGB และ RGBA


สารบัญ

    แสดงสารบัญ


ค่าสี RGB แสดงถึงแสงสีแดง เขียว และสีน้ำเงิน แหล่งที่มา


ค่า RGB

ใน CSS สีสามารถระบุเป็นค่า RGB ได้โดยใช้สูตรนี้:

RGB(แดง, เขียว, น้ำเงิน)

แต่ละพารามิเตอร์ (แดง เขียว และ สีน้ำเงิน) กำหนดความเข้มของสีระหว่าง 0 ถึง 255

ตัวอย่างเช่น rgb(255, 0, 0) จะแสดงเป็นสีแดง เพราะสีแดงถูกกำหนดไว้ที่ค่าสูงสุด (255) และค่าอื่นๆ ก็คือ ตั้งเป็น 0

หากต้องการแสดงสีดำ ให้ตั้งค่าพารามิเตอร์สีทั้งหมดเป็น 0 ดังนี้: rgb(0, 0, 0)

หากต้องการแสดงสีขาว ให้ตั้งค่าพารามิเตอร์สีทั้งหมดเป็น 255 เช่น นี้: rgb (255, 255, 255)

ทดลองโดยผสมค่า RGB ด้านล่าง:

 

RED

255

GREEN

0

BLUE

0

ตัวอย่าง

rgb(255, 0, 0)
rgb(0, 0, 255)
rgb(60, 179, 113)
rgb(238, 130, 238)
rgb(255, 165, 0)
rgb(106, 90, 205)

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>Specify colors using RGB values</h1>

<h2 style="background-color:rgb(255, 0, 0);">rgb(255, 0, 0)</h2>
<h2 style="background-color:rgb(0, 0, 255);">rgb(0, 0, 255)</h2>
<h2 style="background-color:rgb(60, 179, 113);">rgb(60, 179, 113)</h2>
<h2 style="background-color:rgb(238, 130, 238);">rgb(238, 130, 238)</h2>
<h2 style="background-color:rgb(255, 165, 0);">rgb(255, 165, 0)</h2>
<h2 style="background-color:rgb(106, 90, 205);">rgb(106, 90, 205)</h2>

</body>
</html>


เฉดสีเทามักถูกกำหนดโดยใช้ค่าที่เท่ากันสำหรับแหล่งกำเนิดแสงทั้ง 3 ดวง:

ตัวอย่าง

rgb(60, 60, 60)
rgb(90, 90, 90)
rgb(120, 120, 120)
rgb(180, 180, 180)
rgb(210, 210, 210)
rgb(240, 240, 240)

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>Shades of gray</h1>

<p>By using equal values for red, green, and blue, you will get different shades of gray:</p>

<h2 style="background-color:rgb(60, 60, 60);">rgb(60, 60, 60)</h2>
<h2 style="background-color:rgb(90, 90, 90);">rgb(90, 90, 90)</h2>
<h2 style="background-color:rgb(120, 120, 120);">rgb(120, 120, 120)</h2>
<h2 style="background-color:rgb(180, 180, 180);">rgb(180, 180, 180)</h2>
<h2 style="background-color:rgb(210, 210, 210);">rgb(210, 210, 210)</h2>
<h2 style="background-color:rgb(240, 240, 240);">rgb(240, 240, 240)</h2>

</body>
</html>




ค่า RGBA

ค่าสี RGBA เป็นส่วนขยายของค่าสี RGB พร้อมช่องอัลฟา - ซึ่งระบุความทึบของสี

ค่าสี RGBA คือ ระบุด้วย:

RGBA(แดง, เขียว, น้ำเงิน, อัลฟ่า)

พารามิเตอร์อัลฟ่าเป็นตัวเลข ระหว่าง 0.0 (โปร่งใสเต็มที่) ถึง 1.0 (ไม่โปร่งใสเลย):

ทดลองโดยผสมค่า RGBA ด้านล่าง:

 

RED

255

GREEN

0

BLUE

0

ALPHA

0

ตัวอย่าง

rgba(255, 99, 71, 0)
rgba(255, 99, 71, 0.2)
rgba(255, 99, 71, 0.4)
rgba(255, 99, 71, 0.6)
rgba(255, 99, 71, 0.8)
rgba(255, 99, 71, 1)

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>Make transparent colors with RGBA</h1>

<h2 style="background-color:rgba(255, 99, 71, 0);">rgba(255, 99, 71, 0)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.2);">rgba(255, 99, 71, 0.2)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.4);">rgba(255, 99, 71, 0.4)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.6);">rgba(255, 99, 71, 0.6)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.8);">rgba(255, 99, 71, 0.8)</h2>
<h2 style="background-color:rgba(255, 99, 71, 1);">rgba(255, 99, 71, 1)</h2>

</body>
</html>