timofurrer / colorful

Terminal string styling done right, in Python :snake: :tada:
MIT License
525 stars 23 forks source link

How go back to default color pallet #45

Open alfaex opened 3 years ago

alfaex commented 3 years ago

How can I go to default after setting a style?

colorful.use_style('monokai')

print colorful.magenta('color')

# looked on source code and tried these without success
# colorful.use_style('default')
# colorful.use_style('x11')
# colorful.use_style(None)
# colorful.use_style('rgb')
# colorful.use_style('colornames')
# colorful.use_style('X11 rgb.txt')
# colorful.disable()
# colorful.use_8_ansi_colors()
# colorful.use_256_ansi_colors()
# colorful.use_true_colors()

colorful.use_style('TRUE_COLORS')

print colorful.greenYellow('color')

colorful.core.ColorfulAttributeError: the color "greenYellow" is unknown. Use a color in your color palette (by default: X11 rgb.txt)

jmmcd commented 2 months ago

I think I have the same issue.

If I understand the intent of use_style, it is to change the RGB values associated with specific named colours, eg with Solarized, red now means some specific RGB.

But in fact use_style also has the effect of deleting all the other colours.

import colorful as cf
print(cf.darkGreen('darkGreen'))
darkGreen # printed in green
cf.use_style('solarized')
print(cf.darkGreen('darkGreen'))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/colorful/core.py:109, in translate_colorname_to_ansi_code(colorname, offset, colormode, colorpalette)
    108 try:
--> 109     red, green, blue = colorpalette[colorname]
    110 except KeyError:

KeyError: 'darkGreen'

During handling of the above exception, another exception occurred:

ColorfulAttributeError                    Traceback (most recent call last)
Cell In[39], line 1
----> 1 print(cf.darkGreen('darkGreen'))

File /opt/anaconda3/lib/python3.11/site-packages/colorful/__init__.py:122, in ColorfulModule.__getattr__(self, name)
    119     except AttributeError:
    120         pass  # fallback to colorful functionality
--> 122 return getattr(self.colorful, name)

File /opt/anaconda3/lib/python3.11/site-packages/colorful/core.py:549, in Colorful.__getattr__(self, name)
    547 def __getattr__(self, name):
    548     # translate the given name into an ANSI escape code sequence
--> 549     style = translate_style(name, self.colormode, self.colorpalette)
    550     style_wrapper = self.ColorfulStyle(style, self.colormode, self)
    551     return style_wrapper

File /opt/anaconda3/lib/python3.11/site-packages/colorful/core.py:182, in translate_style(style, colormode, colorpalette)
    179 # next part has to be a foreground color or the 'on' keyword
    180 # which means we have to consume background colors
    181 if part != 'on':
--> 182     ansi_start_code, ansi_end_code = translate_colorname_to_ansi_code(
    183         part, ansi.FOREGROUND_COLOR_OFFSET, colormode, colorpalette)
    184     ansi_start_sequence.append(ansi_start_code)
    185     ansi_end_sequence.append(ansi_end_code)

File /opt/anaconda3/lib/python3.11/site-packages/colorful/core.py:111, in translate_colorname_to_ansi_code(colorname, offset, colormode, colorpalette)
    109     red, green, blue = colorpalette[colorname]
    110 except KeyError:
--> 111     raise ColorfulAttributeError('the color "{0}" is unknown. Use a color in your color palette (by default: X11 rgb.txt)'.format(  # noqa
    112         colorname))
    113 else:
    114     return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)

ColorfulAttributeError: the color "darkGreen" is unknown. Use a color in your color palette (by default: X11 rgb.txt)