I have read a couple of URLs about setting colour in terminal. But after a colour change a while later I'd like to reset into previous colour. How can I get current colour ?
(I'd like to avoid third party libraries and use only batteries included ;-))
Especially (from (python) colour printing with decorator in a function ):
import sys
green = '\033[01;32m'
red = '\033[01;31m'
... remember current colours here ...
sys.stdout.write(green+"Hello ")
sys.stderr.write(red+"world!")
You can return default color the same way you colorize your texts:
native = '\033[m'
sys.stdout.write(native)
Thus temporary coloring may be achieved with
print green + 'Hello' + native
Related
I started making a novel and learn renpy really recently, and I came across a difficulty that I couldn't find the solution. I put an input for the user to define the name of the protagonist, but when I declare the character, the color that I designated to appear in the text box is not working.
Here a piece of my code:
python:
sn = ""
define luca = Character(" Luca Kaneshiro", color="#ffffff")
define sn = Character("%(sn)s", color="#ffffff")
label start:
call askname
luca "Hey %(sn)s."
sn "Hi, Luca!"
label askname:
$ sn = renpy.input("What's my name?", length=32)
here's how the text in displayed
I was expecting it to be displayed in white color, but instead it was displayed in yellow, the collor that I defined for gui.accent_color. Is there any solution for that? A way to turn the text in the sn variable into a normal text or something like this?
According to Renpy's documentation on Character, you need to attached a who_style arguments, specifically who_color in this case:
define sn = Character("%(sn)s", who_color="#ffffff")
Also, unrelated but renpy doc uses square bracket for string/text interpolation instead, so in case the above doesn't work, try the following:
define sn = Character("[sn]", who_color="#ffffff")
In most terminals, I can use ANSI-Colour-Codes in Python. e.g. print("\033[92mHello World") would print out Hello World in green. However in other terminals this does not work and it prints the [94mHello World literally.
How can I determine if the terminal that is used can display colours within code. (This is not an issue of determining if an individual terminal can display colours. What I want is to differentiate between terminals within code.)
I used this code:
import crayons
print(crayons.red('red'))
print(crayons.yellow('yellow'))
print(crayons.green('green'))
print(crayons.cyan('cyan'))
print(crayons.blue('blue'))
print(crayons.magenta('magenta'))
print(crayons.white('white'))
print(crayons.black('black'))
To get this:
So now I know what all of the colors look like for my compiler.
I know how to change the font colour of output in Python using this (for example)
print('\033[94m' + text)
However, the rest of the text will then stay that colour, even after the app has closed. I could set it to a certain colour, but the user may have changed the background colour and that would be a problem,
print('\x1b\x63') # Esc c
This will reset the terminal (RIS).
I write a lot of little helper scripts, and often these print coloured text in the terminal. For the simplicity of their packaging and distribution, I often want these little scripts to be without any dependencies.
Hence I'm duplicating data like this a lot in scripts:
ansi_colors = {
None: '\x1b[0m', # actually black but whatevs
'red': '\x1b[31m',
'green' : '\x1b[32m',
...
}
Does this data exist anywhere in the core libraries? I dug around and found that curses has some COLOR_* constants, but they are just integers and it's not obvious how those transform into the ANSI escape codes.
I'm already aware of modules like termcolor, colorama, blessings, so please don't suggest to use those - I want to depend to standard libraries only.
You can check the man page console_codes(4). What you want is the ECMA-48 Set Graphics Rendition:
The ECMA-48 SGR sequence ESC [ parameters m sets display attributes.
Several attributes can be set in the same sequence, separated by
semicolons. An empty parameter (between semicolons or string
initiator or terminator) is
interpreted as a zero.
param result
0 reset all attributes to their defaults
1 set bold
2 set half-bright (simulated with color on a color display)
4 set underscore (simulated with color on a color display) (the colors used to
simulate dim or underline are set using ESC ] ...)
5 set blink
7 set reverse video
10 reset selected mapping, display control flag, and toggle meta flag (ECMA-48
says "primary font").
11 select null mapping, set display control flag, reset toggle meta flag
(ECMA-48 says "first alternate font").
12 select null mapping, set display control flag, set toggle meta flag (ECMA-48
says "second alternate font"). The toggle meta flag causes the high bit of a
byte to be toggled before the mapping table translation is done.
21 set normal intensity (ECMA-48 says "doubly underlined")
22 set normal intensity
24 underline off
25 blink off
27 reverse video off
30 set black foreground
31 set red foreground
32 set green foreground
33 set brown foreground
34 set blue foreground
35 set magenta foreground
36 set cyan foreground
37 set white foreground
38 set underscore on, set default foreground color
39 set underscore off, set default foreground color
40 set black background
41 set red background
42 set green background
43 set brown background
44 set blue background
45 set magenta background
46 set cyan background
47 set white background
49 set default background color
I don't think they are available as-is in any standard Python module. But if you look carefully, you'll notice that the foreground colors are 30 plus the curses constant, while the background colors are 40 plus the curses constant. So you can write something like this:
import curses
def fcolor(c):
return '\x1B[{0}m'.format(30 + c)
def bcolor(c):
return '\x1B[{0}m'.format(40 + c)
def fbcolor(f, b):
return '\x1B[{0};{1}m'.format(30 + f, 40 + b)
print(fbcolor(curses.COLOR_RED, curses.COLOR_YELLOW) + "hello!")
It depends on what you want. ANSI colors technically refers to the 8-color palette implied by ECMA-48 (ISO-6429) which has named constants in curses. Libraries don't store escape sequences; those are in a database. For an ANSI (sic) terminal, those correspond to escape sequences which set graphic rendition (video attributes such as bold, underline, reverse — and color).
termcap, terminfo and curses use a more general notion where you start with a color number and generate the escape sequence which can produce the corresponding color. Terminals can have no colors, multiple colors (8 for instance, but possibly 16, 88, 256 for xterm and similar terminals). The information telling how to do this is stored in the terminal database as named capabilities. To set the ANSI foreground color, you would use setaf, either using a library call or a command-line application such as tput, e.g.,
tput setaf 4
for color 4 (blue). Simple applications use the low-level termcap or terminfo interfaces, usually with terminfo databases. While you might be inclined to format your own escape sequences, theses interfaces provide formatting functions that let you avoid knowing how many colors a terminal might support. The terminal database tells your program, using the TERM environment variable to select the actual terminal description. If you have a terminal supporting more than 8 colors, the escape sequence is not formed by adding 30 or 40 to the color number.
Here is an example using the low-level terminfo interface from Python:
import curses
curses.setupterm()
curses.putp(curses.tparm(curses.tigetstr("setaf"), curses.COLOR_RED))
curses.putp(curses.tparm(curses.tigetstr("setab"), curses.COLOR_YELLOW))
curses.putp("hello!")
curses.putp(curses.tigetstr("sgr0"))
curses.putp("\n")
Further reading:
15.11. curses — Terminal handling for character-cell displays
ECMA-48: Control Functions for Coded Character Sets
Rodrigo gave a good answer, although the colors are limited to the 8 first colors for foreground. This is my little contribution to handle 16 foreground colors
def fcolor(c):
if c>7:
return '\x1B[1;{0}m'.format(22 + c)
else:
return '\x1B[0;{0}m'.format(30 + c)
def bcolor(c):
return '\x1B[{0}m'.format(40 + c)
def fbcolor(f, b):
if f>7:
return '\x1B[1;{0};{1}m'.format(22 + f, 40 + b)
else:
return '\x1B[0;{0};{1}m'.format(30 + f, 40 + b)
Is the following function correct, this code is meant to add a phrase to image. Note that i cannot use image.text function or any other but can only use getpixel, putpixel, load, and save.
def insertTxtImage(srcImage, phrase):
pixel = srcImage.getpixel(30,30);
srcImage.putpixel(pixel,phrase);
srcImage.save;
pass
Yes it is homework which can only use getpixel, putpixel, load, and save to insert a phrase in to the image.
I tried to do this with this code but it is giving system error (argument is not a tuple)
def insertTxtImage(srcImage, phrase):
pix = srcImage.load()
pix[0,0] = phrase
srcImage.save()
pass
Thanks for the comments.
No, the functions you are using modify pixels.
To draw font you want to use something like following:
f= pygame.font.Font(None, 12)
surf= f.render(phrase)
srcImage.blit(surf, (30,30))
for more documentation see here: (scroll down a bit)
http://www.pygame.org/docs/ref/font.html
EDIT: nvm, I don't even know what you're doing or trying to do