Changeset - 57a095aafd9b
[Not reviewed]
0 3 0
Branko Majic (branko) - 7 years ago 2016-12-03 13:26:56
branko@majic.rs
PYD-6: Switched to using RGBA in order to support transparency out of the box. Updated usage instructions to include info on how to specify transparency. Updated tests to accomodate to change in colour scheme used in images.
3 files changed with 25 insertions and 7 deletions:
0 comments (0 inline, 0 general)
docs/usage.rst
Show inline comments
 
@@ -107,6 +107,24 @@ to achieve::
 
  # ASCII identicon can be printed-out to console directly.
 
  print identicon_ascii
 

	
 

	
 
Working with transparency
 
-------------------------
 

	
 
.. note::
 
   New in version ``0.3``.
 

	
 
If you ever find yourself in need of having a transparent background or
 
foreground, you can easily do this using the syntax
 
``rgba(224,224,224,0)``. All this does is effectively adding alpha channel to
 
selected colour.
 

	
 
The alpha channel value ranges from ``0`` to ``255``, letting you specify how
 
much transparency/opaqueness you want. For example, to have it at roughly 50%
 
(more like at ``50.2%`` since you can't use fractions), you would simply specify
 
value as ``rgba(224,224,224,128)``.
 

	
 

	
 
Full example
 
------------
 

	
pydenticon/__init__.py
Show inline comments
 
@@ -218,7 +218,7 @@ class Generator(object):
 
        """
 

	
 
        # Set-up a new image object, setting the background to provided value.
 
        image = Image.new("RGB", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background)
 
        image = Image.new("RGBA", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background)
 

	
 
        # Set-up a draw image (for drawing the blocks).
 
        draw = ImageDraw.Draw(image)
tests/test_pydenticon.py
Show inline comments
 
@@ -189,7 +189,7 @@ class GeneratorTest(unittest.TestCase):
 
        self.assertEqual(image.size[0], 240)
 
        self.assertEqual(image.size[1], 240)
 
        self.assertEqual(image.format, "PNG")
 
        self.assertEqual(image.mode, "RGB")
 
        self.assertEqual(image.mode, "RGBA")
 

	
 
    def test_generate_ascii(self):
 
        """
 
@@ -359,10 +359,10 @@ class GeneratorTest(unittest.TestCase):
 
        height = 200
 
        padding = (20, 20, 20, 20)
 

	
 
        # Load the reference images, making sure they're in RGB mode.
 
        test1_ref = PIL.Image.open("tests/samples/test1.png").convert(mode="RGB")
 
        test2_ref = PIL.Image.open("tests/samples/test2.png").convert(mode="RGB")
 
        test3_ref = PIL.Image.open("tests/samples/test3.png").convert(mode="RGB")
 
        # Load the reference images, making sure they're in RGBA mode.
 
        test1_ref = PIL.Image.open("tests/samples/test1.png").convert(mode="RGBA")
 
        test2_ref = PIL.Image.open("tests/samples/test2.png").convert(mode="RGBA")
 
        test3_ref = PIL.Image.open("tests/samples/test3.png").convert(mode="RGBA")
 

	
 
        # Set-up the Generator.
 
        generator = Generator(5, 5, foreground=foreground, background=background)
 
@@ -389,7 +389,7 @@ class GeneratorTest(unittest.TestCase):
 

	
 
        # Verify that all the diffs are essentially black (i.e. no differences
 
        # between generated identicons and reference samples).
 
        expected_extrema = ((0, 0), (0, 0), (0, 0))
 
        expected_extrema = ((0, 0), (0, 0), (0, 0), (0, 0))
 

	
 
        self.assertEqual(diff1.getextrema(), expected_extrema)
 
        self.assertEqual(diff2.getextrema(), expected_extrema)
0 comments (0 inline, 0 general)