使用24位(HTML5)RGB代码的Python 3打印文本

时间:2018-12-17 16:03:34

标签: python-3.x colors rgb textstyle

在我的脚本中,我希望输入rgb(红色,绿色,蓝色)颜色代码24bit(HTML5)作为背景色和文本颜色。但是我无法正常工作。产生的颜色代码与我从w3schools拾色器复制的颜色代码不同。

See my Repl.it code example或以下代码:

from sty import fg, bg, ef, rs, Rule
import time
import sys

def isInteger(n): #function from the internet that checks if a float is a int
    if n%2 == 0 or (n+1)%2 == 0:
        return True
    return False

def make_int(value):
  try:
    int(value)
    return value
  except:
    return 0

# Function to ask rgb color code : “(#red, #green, #blue)” where #red, #green and #blue should be 0-255
# return this as list variable : color_code = [ #red, #green, #blue ] 

def input_color(question):
  ValidInput = False
  while not ValidInput:
    color = input(question)
    if ( len(color) < 5 ) or ( color.count(",") != 2 ):
      ValidInput = False
    else:
      color_code = color.replace("(", "").replace(")", "").replace("rgb","").split(",")
      IsOK = input("OK? (y/n)")
      ValidInput = ( len(IsOK)==0 or IsOK=="y" )
  return color_code

from termcolor import colored, cprint

# Initial first Elements

elements = ['fire', '\x1b[48;2;204;41;0m\x1b[38;2;255;153;0m\x1b[1mfire\x1b[39m', 'water', '\x1b[48;2;51;204;255m\x1b[38;2;51;51;255m\x1b[1mwater\x1b[39m', 'earth', '\x1b[48;2;153;77;0m\x1b[38;2;77;38;0m\x1b[1mearth\x1b[39m', 'air', '\x1b[48;2;51;204;255m\x1b[38;2;255;255;255m\x1b[1mair\x1b[39m']

# Loop forever : 1. print elements-list, 2. Ask for new element with text color combination

while True:

# 1. print elements-list
  for i in range( int(len(elements) / 2)):
    print("")
    print(elements[i * 2 + 1])
  print("")

# 2. Ask for new element with text color combination
# Ask for a new element and check if name is already used
# if not then ask for text-background (color_b) and text color (color_t) and store the combination in the elements-list 
  name = input(bg.black + fg.white + ef.bold + 'new elements name:' + fg.rs)
  if name in elements:
    print(bg.black + fg.white + ef.bold + 'name already used' + fg.rs)

  else:
    color_b = input_color(bg.black + fg.white + ef.bold + 'rgb for the background:' + fg.rs)
    color_t = input_color(bg.black + fg.white + ef.bold + 'rgb for the text:' + fg.rs)

    elements.append(name)
    elements.append(bg(color_b[0], color_b[1], color_b[2] ) + fg(color_t[0], color_t[1], color_t[2]) + ef.bold + name + fg.rs)

0 个答案:

没有答案