

이 프로젝트는 색상 텍스트 출력뿐만 아니라 텍스트에 그라디언트와 같은 색상 효과를 적용 할 수있는 Python PYPI 패키지를 개발하는 프로젝트입니다.
이 패키지는 ANSI Escape Sect Select Graphics Renditions를 사용하여 텍스트 및 배경에 색상과 스타일을 적용합니다. 이 패키지는 다른 패키지와 어떻게 다른가요?
다른 터미널은 색상을 다르게 렌더링하고 일부 터미널은 ANSI 이스케이프 시퀀스를 전혀 렌더링하지 않아 텍스트의 큰 혼란을 초래합니다. Ansi Escape 코드를 가지고 노는 것은 바로 그 일입니다.
다른 로깅 레벨을 강조하기 위해 드물게 사용되는 일부 기본 색상은 괜찮지 만 심각한 프로젝트에서 색상으로 옮길 수는 없습니다.
먼저, 4 비트 및 8 비트 코드를 사용할 수 있으며 colour_fx.four_bit 및 colour_fx.eight_bit 에서 각각 가져옵니다. 그러나 8 비트 및 4 비트 코드는 모두 문제없이 함께 사용할 수 있습니다.
이 모듈의 객체는 ANSI 이스케이프 시퀀스 내에서 사용될 값 만 제공합니다. comour_ansi_code에서 가져올 수있는 compile_ansi_code 사용하여 ANSI 탈출 시퀀스로 컴파일해야합니다. 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 사용하여 Python 3.9 (가장 낮은 지원되는 Python 버전)로 Venv 설정을 받으십시오.
lint-cfx 명령을 사용하여 작업을 수행하고 test-cfx 사용하여 작업을 테스트하십시오.
이름의 지점을 사용하여 기능 작업을 수행 한 다음 풀 요청을 제출하십시오.