================== Working with color ================== Couple of methods in Pystacia use color as argument. There are many ways to factory a color in Pystacia. All the machinery is defined in :mod:`pystcia.color` As a convention all the channel information for red, blue, green, alpha and so on is specified as ``float`` numbers between `0` and `1`. It can be misleading for people used to thinking in 8-bit 0-255 mode. Pystacia uses ``float`` because of internal 16-bit precision that :term:`ImageMagick` works in and also its `C` interface uses `float`. You normally don't instantiate :class:`pystacia.color.Color` directly. You should rely on factory methods presented below instead. Creating colors from string =========================== :func:`pystacia.color.from_string` can be used to synthesize colors from their string representations. It accepts broad variety of input formats as defined in :term:`CSS3` `color module `_. Well known colors: >>> color.from_string('transparent') >>> color.from_string('red') >>> color.from_string('teal') Function syntax: >>> color.from_string('rgb(255, 255, 255)') # rgb >>> color.from_string('rgb(100%,100%,50%)') # rgb with percentage >>> color.from_string('rgba(255, 255, 255, 0.5)') # rgba >>> color.from_string('hsl(120, 100%, 75%)') # hsl >>> color.from_string('hsla(240, 100%, 50%, 0.5)') # hsla Creating colors from floats =========================== It's not always convenient to use string syntax. You can use :func:`pystacia.color.from_rgb` and :func:`pystacia.color.from_rgba` to create colors from numerical values. >>> color.from_rgb(0, 1, 1) >>>> color.from_rgba(0.5, 0.5, 0.5, 0.5) You can also create colors by probing them from images with :meth:`pystacia.image.Image.get_pixel`. Color class =========== Once instantiated a :class:`pystacia.color.Color` instance can be queried and modified. Channel information ------------------- Red, blue, green and alpha information can be accessed and modified with :attr:`pystacia.color.Color.red`, :attr:`pystacia.color.Color.green`, :attr:`pystacia.color.Color.blue`, :attr:`pystacia.color.Color.alpha` properties that also have convenience one letter abbreviations: :attr:`pystacia.color.Color.r`, :attr:`pystacia.color.Color.g`, :attr:`pystacia.color.Color.b`, :attr:`pystacia.color.Color.a`. >>> red = color.from_string('red') >>> red.red 1 >>> red.red == red.r True >>> red.green 0 >>> red.green = 1 >>> red.g 1 >>> red.a = 0.5 >>> red You can also set several channels at once with :meth:`pystacia.color.Color.set_rgb` and :meth:`pystacia.color.Color.set_rgba` methods: >>> red.set_rgb(0, 0.5, 1) >>> red >>> red.set_rgba(1, 1, 1, 0.1) >>> red Also access all channels at once as tuples with :meth:`pystacia.color.Color.get_rgb` and :meth:`pystacia.color.Color.get_rgba`: >>> red.get_rgb() (1, 1, 1) >>> red.get_rgba() (1, 1, 1, 0.1) To return value :term:`CSS3` string representation of color use :meth:`pystacia.color.Color.get_string` or cast instance with :func:`str`: >>> red.get_string() 'rgba(255, 255, 255, 0.1)' >>> str(red) 'rgba(255, 255, 255, 0.1)' Testing for transparency ------------------------ You can query if color is fully transparent with :attr:`pystacia.color.Color.transparent` property whilst you can use :attr:`pystacia.color.Color.opaque` to test if color is fully opaque. >>> red = color.from_string('red') >>> red.opaque True >>> red.transparent False >>> transparent = color.from_string('transparent') >>> transparent.opaque False >>> transparent.transparent True