

นี่เป็นโครงการในการพัฒนาแพ็คเกจ Python PYPI ที่ไม่เพียง แต่จัดการเอาต์พุตข้อความสี แต่ยังสามารถใช้เอฟเฟกต์สีเช่นการไล่ระดับสีกับข้อความของคุณ
แพ็คเกจนี้ใช้ ANSI Escape Sequence เลือกการแสดงกราฟิกเพื่อใช้สีและสไตล์กับข้อความและพื้นหลัง แพ็คเกจนี้แตกต่างกับคนอื่นอย่างไร?
เทอร์มินัลที่แตกต่างกันทำให้สีแตกต่างกันเทอร์มินัลบางตัวไม่ได้ทำให้ ANSI Escape Sequences เลยนำไปสู่ข้อความที่ยุ่งเหยิง การเล่นรอบ ๆ ด้วยรหัส Escape Ansi นั้นเป็นเช่นนั้นมันกำลังเล่นอยู่
สีพื้นฐานบางอย่างที่ใช้เท่าที่จำเป็นเพื่อเน้นระดับการบันทึกที่แตกต่างกันนั้นดี แต่คุณไม่สามารถใช้สีในโครงการที่จริงจังได้
ก่อนอื่นมีรหัส 4 บิตและ 8 บิตนำเข้าจาก colour_fx.four_bit และ colour_fx.eight_bit ตามลำดับ อย่างไรก็ตามรหัสทั้ง 8 บิตและ 4 บิตสามารถใช้ร่วมกันได้โดยไม่มีปัญหา
วัตถุในโมดูลเหล่านี้ให้ค่าที่จะใช้ภายในลำดับ ANSI Escape เท่านั้น พวกเขาจะต้องรวบรวมลงในลำดับ ANSI Escape โดยใช้ compile_ansi_code ซึ่งสามารถนำเข้าได้ from colour_fx import compile_ansi_code
from colour_fx import compile_ansi_code
from colour_fx . four_bit import Colour , Style
blinking_red_on_yellow = compile_ansi_code (
# add as many ANSI values as you like and receive a single ANSI
# escape sequence to handle all effects
Colour . RED ,
Colour . YELLOW . bright_background ,
Style . BLINK
)
# an empty call to compile_ansi_code() gives the reset ANSI escape
# sequence which resets the way text is rendered
RESET = compile_ansi_code ()
print ( F"HELLO, { blinking_red_on_yellow } WORLD { RESET } !" )
สี 8 บิตนั้นไม่สามารถคาดเดาได้ในลักษณะที่แตกต่างกันระหว่างเทอร์มินัลที่แตกต่างกันมากกว่าสี 4 บิต
SimpleColour from colour_fx import compile_ansi_code
from colour_fx . eight_bit import SimpleColour
red_on_yellow = compile_ansi_code (
# add as many ANSI values as you like and receive a single ANSI
# escape sequence to handle all effects
SimpleColour . RED ,
SimpleColour . YELLOW . bright_background ,
)
# an empty call to compile_ansi_code() gives the reset ANSI escape
# sequence which resets the way text is rendered
RESET = compile_ansi_code ()
print ( F"HELLO, { red_on_yellow } WORLD { RESET } !" )
Grey from colour_fx import compile_ansi_code
from colour_fx . eight_bit import Grey
# Grey has 24 different shades available, here we use 12 to apply a
# gradient to the text "HELLO, WORLD!"
grey_array = [
Grey . AA ,
Grey . AC ,
Grey . AE ,
Grey . BB ,
Grey . BD ,
Grey . BF ,
Grey . CA ,
Grey . CC ,
Grey . CE ,
Grey . DB ,
Grey . DD ,
Grey . DF ,
]
# converting shades in grey_array to background colours and
# reversing order
grey_back = [ grey . background for grey in reversed ( grey_array )]
# Compiling the values in grey_array and grey_back into codes
g = [
compile_ansi_code (
grey_array [ idx ],
grey_back [ idx ],
)
for idx in range ( len ( grey_array ))
]
# an empty call to compile_ansi_code() gives the reset ANSI escape
# sequence which resets the way text is rendered
RESET = compile_ansi_code ()
# Accessing the individual ANSI escape codes in the list of codes to
# create a gradient
print (
F" { g [ 0 ] } H { g [ 1 ] } E { g [ 2 ] } L { g [ 3 ] } L { g [ 4 ] } O { g [ 5 ] } ,"
F" { g [ 6 ] } W { g [ 7 ] } O { g [ 8 ] } R { g [ 9 ] } L { g [ 10 ] } D { g [ 11 ] } ! { RESET } "
)
RGB from colour_fx import compile_ansi_code
from colour_fx . eight_bit import RGB
# RGB is different to the rest in that you need to pass values in and
# initialise the object rather than treating as an Enum
# RGB values should be an integer between 0 and 5 inclusive
spectrum = [
RGB ( 5 , 0 , 0 ). foreground ,
RGB ( 3 , 2 , 0 ). foreground ,
RGB ( 1 , 4 , 0 ). foreground ,
RGB ( 0 , 4 , 1 ). foreground ,
RGB ( 0 , 2 , 3 ). foreground ,
RGB ( 0 , 0 , 5 ). foreground ,
]
# compiling spectrum into ANSI escape sequences while adding a
# white background
s = [
compile_ansi_code ( spec , RGB ( 5 , 5 , 5 ). background )
for spec in spectrum
]
# an empty call to compile_ansi_code() gives the reset ANSI escape
# sequence which resets the way text is rendered
RESET = compile_ansi_code ()
# Accessing individual elements of s allows a gradient
print ( F" { s [ 0 ] } HE { s [ 1 ] } LL { s [ 2 ] } O, { s [ 3 ] } WO { s [ 4 ] } RL { s [ 5 ] } D! { RESET } " )
ตอนนี้นี่คือสิ่งที่เป็นแรงบันดาลใจในการสร้างแพ็คเกจนี้สามารถใช้การไล่ระดับสีกับข้อความขนาดใหญ่
from colour_fx . four_bit import Colour
from colour_fx . effects . gradients import create_vertical_gradient_field
from colour_fx . effects import apply_ansi_field
rainbow_vals = [
[ Colour . RED ],
[ Colour . RED . bright ],
[ Colour . YELLOW . bright ],
[ Colour . GREEN ],
[ Colour . BLUE ],
[ Colour . BLUE . bright ],
[ Colour . MAGENTA ],
]
text_to_render = (
" ::: ::: :::::::::: ::: ::: :::::::: ::: n "
" :+: :+: :+: :+: :+: :+: :+: :+: n "
" +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ n "
" +#++:++#++ +#++:++# +#+ +#+ +#+ +:+ +#+ n "
" +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ n "
" #+# #+# #+# #+# #+# #+# #+# n "
" ### ### ########## ########## ########## ######## ### n "
)
output_field = create_vertical_gradient_field (
template = text_to_render ,
ansi_vals = rainbow_vals ,
indent = 3 ,
step = 1 ,
)
output_text = apply_ansi_field (
text = text_to_render ,
field = output_field
)
print ( output_text )
อย่าใช้ Windows ติดตั้ง Python 3.9 จากนั้นใช้ source ./tooling && venv-setup เพื่อให้ตัวเองติดตั้ง VENV ด้วย Python 3.9 (เวอร์ชัน Python ที่รองรับต่ำสุดของเรา)
ใช้คำสั่ง lint-cfx เพื่องานผ้าสำลีและคำสั่ง test-cfx เพื่อทดสอบงาน
ใช้สาขาในชื่อของคุณเพื่อทำงานคุณลักษณะจากนั้นส่งคำขอดึง