ที่นี่เราจะดูวิธีพิมพ์ข้อความบางบรรทัดในเทอร์มินัล Linux พร้อมสี เราไม่ได้ทำอะไรพิเศษในโค้ด C++ เราเพียงแค่ใช้คำสั่งเทอร์มินัล Linux บางอย่าง คำสั่งสำหรับเอาต์พุทแบบนี้เป็นดังนี้
\033[1;31m Sample Text \033[0m
มีรหัสสำหรับสไตล์และสีข้อความ รายการดังนี้
| สี (Color) | รหัส Foreground | รหัส Background |
|---|---|---|
| Black | 30 | 40 |
| Red | 31 | 41 |
| Green | 32 | 42 |
| Yellow | 33 | 43 |
| Blue | 34 | 44 |
| Magenta | 35 | 45 |
| Cyan | 36 | 46 |
| White | 37 | 47 |
ตัวเลือกเพิ่มเติมมีดังนี้ -
| ตัวเลือก (Option) | รหัส (Code) | คำอธิบาย |
|---|---|---|
| Reset | 0 | กลับสู่ปกติ (ลบสไตล์ทั้งหมด) |
| Bold | 1 | ทำให้ข้อความหนา |
| Underline | 4 | ขีดเส้นใต้ข้อความ |
| Inverse | 7 | สลับสีพื้นหลังและข้อความ |
| Bold off | 21 | ปกติจาก Bold |
| Underline off | 24 | ปกติจาก Underline |
| Inverse off | 27 | ย้อนกลับจาก Inverse |
ตัวอย่าง
#include<iostream> using namespace std; main() { cout << "\033[1;31mThis is bold red text\033[0m\n"; cout << "\033[;32mGreen Text\033[0m\n"; cout << "\033[4;33mYellow underlined text\033[0m\n"; cout << "\033[;34mBlue text\033[0m\n"; }
ผลลัพธ์
