2013-08-27 11:47:14 +02:00
#!/usr/bin/env python
# -*- coding:utf-8 -*-
2013-09-16 21:08:55 +02:00
###
# qidenticon.py is Licesensed under FreeBSD License.
# (http://www.freebsd.org/copyright/freebsd-license.html)
#
# Copyright 2013 "Sendiulo". All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###
###
# identicon.py is Licesensed under FreeBSD License.
# (http://www.freebsd.org/copyright/freebsd-license.html)
#
# Copyright 1994-2009 Shin Adachi. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###
2013-08-27 11:47:14 +02:00
"""
2013-09-16 21:08:55 +02:00
qidenticon . py
identicon python implementation with QPixmap output
by sendiulo < sendiulo @gmx.net >
based on
2013-08-27 11:47:14 +02:00
identicon . py
identicon python implementation .
by Shin Adachi < shn @glucose.jp >
= usage =
== python ==
2013-09-16 21:08:55 +02:00
>> > import qtidenticon
>> > qtidenticon . render_identicon ( code , size )
2013-08-27 11:47:14 +02:00
Return a PIL Image class instance which have generated identicon image .
` ` ` size ` ` ` specifies ` patch size ` . Generated image size is 3 * ` ` ` size ` ` ` .
"""
2018-02-15 18:28:01 +01:00
from qtpy import QtCore , QtGui
2013-08-27 11:47:14 +02:00
__all__ = [ ' render_identicon ' , ' IdenticonRendererBase ' ]
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
class IdenticonRendererBase ( object ) :
PATH_SET = [ ]
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
def __init__ ( self , code ) :
"""
@param code code for icon
"""
if not isinstance ( code , int ) :
code = int ( code )
self . code = code
2018-02-15 18:28:01 +01:00
2013-09-18 17:39:45 +02:00
def render ( self , size , twoColor , opacity , penwidth ) :
2013-08-27 11:47:14 +02:00
"""
2013-09-18 17:39:45 +02:00
render identicon to QPicture
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
@param size identicon patchsize . ( image size is 3 * [ size ] )
2013-09-18 17:39:45 +02:00
@return QPicture
2013-08-27 11:47:14 +02:00
"""
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
# decode the code
2018-02-15 18:28:01 +01:00
middle , corner , side , foreColor , secondColor , swap_cross = \
self . decode ( self . code , twoColor )
2013-08-27 11:47:14 +02:00
2013-09-16 21:08:55 +02:00
# make image
2018-02-15 18:28:01 +01:00
image = QtGui . QPixmap (
QtCore . QSize ( size * 3 + penwidth , size * 3 + penwidth ) )
2013-08-27 11:47:14 +02:00
# fill background
2018-02-15 18:28:01 +01:00
backColor = QtGui . QColor ( 255 , 255 , 255 , opacity )
2013-09-17 10:55:26 +02:00
image . fill ( backColor )
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
kwds = {
2013-09-16 21:08:55 +02:00
' image ' : image ,
2013-08-27 11:47:14 +02:00
' size ' : size ,
2013-09-17 10:55:26 +02:00
' foreColor ' : foreColor if swap_cross else secondColor ,
' penwidth ' : penwidth ,
2013-08-27 11:47:14 +02:00
' backColor ' : backColor }
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
# middle patch
2018-02-15 18:28:01 +01:00
image = self . drawPatchQt (
( 1 , 1 ) , middle [ 2 ] , middle [ 1 ] , middle [ 0 ] , * * kwds )
2013-08-27 11:47:14 +02:00
# side patch
2013-09-17 10:55:26 +02:00
kwds [ ' foreColor ' ] = foreColor
2013-08-27 11:47:14 +02:00
kwds [ ' type ' ] = side [ 0 ]
for i in xrange ( 4 ) :
pos = [ ( 1 , 0 ) , ( 2 , 1 ) , ( 1 , 2 ) , ( 0 , 1 ) ] [ i ]
2013-09-16 21:08:55 +02:00
image = self . drawPatchQt ( pos , side [ 2 ] + 1 + i , side [ 1 ] , * * kwds )
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
# corner patch
2013-09-17 10:55:26 +02:00
kwds [ ' foreColor ' ] = secondColor
2013-08-27 11:47:14 +02:00
kwds [ ' type ' ] = corner [ 0 ]
for i in xrange ( 4 ) :
pos = [ ( 0 , 0 ) , ( 2 , 0 ) , ( 2 , 2 ) , ( 0 , 2 ) ] [ i ]
2013-09-16 21:08:55 +02:00
image = self . drawPatchQt ( pos , corner [ 2 ] + 1 + i , corner [ 1 ] , * * kwds )
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
return image
2013-09-16 21:08:55 +02:00
2018-02-15 18:28:01 +01:00
def drawPatchQt (
self , pos , turn , invert , type , image , size , foreColor ,
2013-09-17 10:55:26 +02:00
backColor , penwidth ) :
2013-08-27 11:47:14 +02:00
"""
@param size patch size
"""
path = self . PATH_SET [ type ]
if not path :
# blank patch
invert = not invert
path = [ ( 0. , 0. ) , ( 1. , 0. ) , ( 1. , 1. ) , ( 0. , 1. ) , ( 0. , 0. ) ]
2013-09-16 21:08:55 +02:00
2018-02-15 18:28:01 +01:00
polygon = QtGui . QPolygonF ( [
QtCore . QPointF ( x * size , y * size ) for x , y in path ] )
2013-09-16 21:08:55 +02:00
rot = turn % 4
2018-02-15 18:28:01 +01:00
rect = [
QtCore . QPointF ( 0. , 0. ) , QtCore . QPointF ( size , 0. ) ,
QtCore . QPointF ( size , size ) , QtCore . QPointF ( 0. , size ) ]
rotation = [ 0 , 90 , 180 , 270 ]
nopen = QtGui . QPen ( foreColor , QtCore . Qt . NoPen )
foreBrush = QtGui . QBrush ( foreColor , QtCore . Qt . SolidPattern )
2013-09-17 10:55:26 +02:00
if penwidth > 0 :
2013-09-18 17:39:45 +02:00
pen_color = QtGui . QColor ( 255 , 255 , 255 )
2018-02-15 18:28:01 +01:00
pen = QtGui . QPen ( pen_color , QtCore . Qt . SolidPattern )
2013-09-17 10:55:26 +02:00
pen . setWidth ( penwidth )
2018-02-15 18:28:01 +01:00
painter = QtGui . QPainter ( )
2013-09-16 21:08:55 +02:00
painter . begin ( image )
2013-09-17 10:55:26 +02:00
painter . setPen ( nopen )
2018-02-15 18:28:01 +01:00
painter . translate ( pos [ 0 ] * size + penwidth / 2 , pos [ 1 ] * size + penwidth / 2 )
2013-09-17 10:55:26 +02:00
painter . translate ( rect [ rot ] )
2013-09-16 21:08:55 +02:00
painter . rotate ( rotation [ rot ] )
2018-02-15 18:28:01 +01:00
2013-09-17 10:55:26 +02:00
if invert :
# subtract the actual polygon from a rectangle to invert it
2018-02-15 18:28:01 +01:00
poly_rect = QtGui . QPolygonF ( rect )
2013-09-18 17:39:45 +02:00
polygon = poly_rect . subtracted ( polygon )
2013-09-16 21:08:55 +02:00
painter . setBrush ( foreBrush )
2013-09-17 10:55:26 +02:00
if penwidth > 0 :
# draw the borders
painter . setPen ( pen )
2018-02-15 18:28:01 +01:00
painter . drawPolygon ( polygon , QtCore . Qt . WindingFill )
2013-09-17 10:55:26 +02:00
# draw the fill
painter . setPen ( nopen )
2018-02-15 18:28:01 +01:00
painter . drawPolygon ( polygon , QtCore . Qt . WindingFill )
2013-09-16 21:08:55 +02:00
painter . end ( )
2018-02-15 18:28:01 +01:00
2013-09-16 21:08:55 +02:00
return image
2013-08-27 11:47:14 +02:00
2018-02-15 18:28:01 +01:00
# virtual functions
2013-08-27 11:47:14 +02:00
def decode ( self , code ) :
raise NotImplementedError
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
class DonRenderer ( IdenticonRendererBase ) :
"""
Don Park ' s implementation of identicon
see : http : / / www . docuverse . com / blog / donpark / 2007 / 01 / 19 / identicon - updated - and - source - released
"""
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
PATH_SET = [
2018-02-15 18:28:01 +01:00
# [0] full square:
2013-09-18 17:39:45 +02:00
[ ( 0 , 0 ) , ( 4 , 0 ) , ( 4 , 4 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [1] right-angled triangle pointing top-left:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 4 , 0 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [2] upwardy triangle:
2013-08-27 11:47:14 +02:00
[ ( 2 , 0 ) , ( 4 , 4 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [3] left half of square, standing rectangle:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 2 , 0 ) , ( 2 , 4 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [4] square standing on diagonale:
2013-09-18 17:39:45 +02:00
[ ( 2 , 0 ) , ( 4 , 2 ) , ( 2 , 4 ) , ( 0 , 2 ) ] ,
2018-02-15 18:28:01 +01:00
# [5] kite pointing topleft:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 4 , 2 ) , ( 4 , 4 ) , ( 2 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [6] Sierpinski triangle, fractal triangles:
2013-08-27 11:47:14 +02:00
[ ( 2 , 0 ) , ( 4 , 4 ) , ( 2 , 4 ) , ( 3 , 2 ) , ( 1 , 2 ) , ( 2 , 4 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [7] sharp angled lefttop pointing triangle:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 4 , 2 ) , ( 2 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [8] small centered square:
2013-09-18 17:39:45 +02:00
[ ( 1 , 1 ) , ( 3 , 1 ) , ( 3 , 3 ) , ( 1 , 3 ) ] ,
2018-02-15 18:28:01 +01:00
# [9] two small triangles:
2013-08-27 11:47:14 +02:00
[ ( 2 , 0 ) , ( 4 , 0 ) , ( 0 , 4 ) , ( 0 , 2 ) , ( 2 , 2 ) ] ,
2018-02-15 18:28:01 +01:00
# [10] small topleft square:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 2 , 0 ) , ( 2 , 2 ) , ( 0 , 2 ) ] ,
2018-02-15 18:28:01 +01:00
# [11] downpointing right-angled triangle on bottom:
2013-08-27 11:47:14 +02:00
[ ( 0 , 2 ) , ( 4 , 2 ) , ( 2 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [12] uppointing right-angled triangle on bottom:
2013-08-27 11:47:14 +02:00
[ ( 2 , 2 ) , ( 4 , 4 ) , ( 0 , 4 ) ] ,
2018-02-15 18:28:01 +01:00
# [13] small rightbottom pointing right-angled triangle on topleft:
2013-08-27 11:47:14 +02:00
[ ( 2 , 0 ) , ( 2 , 2 ) , ( 0 , 2 ) ] ,
2018-02-15 18:28:01 +01:00
# [14] small lefttop pointing right-angled triangle on topleft:
2013-08-27 11:47:14 +02:00
[ ( 0 , 0 ) , ( 2 , 0 ) , ( 0 , 2 ) ] ,
2018-02-15 18:28:01 +01:00
# [15] empty:
2013-09-18 17:39:45 +02:00
[ ] ]
2018-02-15 18:28:01 +01:00
# get the [0] full square, [4] square standing on diagonale,
# [8] small centered square, or [15] empty tile:
2013-08-27 11:47:14 +02:00
MIDDLE_PATCH_SET = [ 0 , 4 , 8 , 15 ]
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
# modify path set
for idx in xrange ( len ( PATH_SET ) ) :
if PATH_SET [ idx ] :
p = map ( lambda vec : ( vec [ 0 ] / 4.0 , vec [ 1 ] / 4.0 ) , PATH_SET [ idx ] )
PATH_SET [ idx ] = p + p [ : 1 ]
2018-02-15 18:28:01 +01:00
2013-09-17 10:55:26 +02:00
def decode ( self , code , twoColor ) :
# decode the code
shift = 0 ; middleType = ( code >> shift ) & 0x03
shift + = 2 ; middleInvert = ( code >> shift ) & 0x01
shift + = 1 ; cornerType = ( code >> shift ) & 0x0F
shift + = 4 ; cornerInvert = ( code >> shift ) & 0x01
shift + = 1 ; cornerTurn = ( code >> shift ) & 0x03
shift + = 2 ; sideType = ( code >> shift ) & 0x0F
shift + = 4 ; sideInvert = ( code >> shift ) & 0x01
shift + = 1 ; sideTurn = ( code >> shift ) & 0x03
shift + = 2 ; blue = ( code >> shift ) & 0x1F
shift + = 5 ; green = ( code >> shift ) & 0x1F
shift + = 5 ; red = ( code >> shift ) & 0x1F
shift + = 5 ; second_blue = ( code >> shift ) & 0x1F
shift + = 5 ; second_green = ( code >> shift ) & 0x1F
shift + = 5 ; second_red = ( code >> shift ) & 0x1F
shift + = 1 ; swap_cross = ( code >> shift ) & 0x01
2018-02-15 18:28:01 +01:00
2013-09-17 10:55:26 +02:00
middleType = self . MIDDLE_PATCH_SET [ middleType ]
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
foreColor = ( red << 3 , green << 3 , blue << 3 )
2013-09-16 21:08:55 +02:00
foreColor = QtGui . QColor ( * foreColor )
2018-02-15 18:28:01 +01:00
2013-09-17 10:55:26 +02:00
if twoColor :
2018-02-15 18:28:01 +01:00
secondColor = (
second_blue << 3 , second_green << 3 , second_red << 3 )
2013-09-17 10:55:26 +02:00
secondColor = QtGui . QColor ( * secondColor )
else :
secondColor = foreColor
2018-02-15 18:28:01 +01:00
2013-08-27 11:47:14 +02:00
return ( middleType , middleInvert , 0 ) , \
( cornerType , cornerInvert , cornerTurn ) , \
( sideType , sideInvert , sideTurn ) , \
2013-09-17 10:55:26 +02:00
foreColor , secondColor , swap_cross
2013-08-27 11:47:14 +02:00
2018-02-15 18:28:01 +01:00
def render_identicon (
code , size , twoColor = False , opacity = 255 , penwidth = 0 , renderer = None ) :
2013-08-27 11:47:14 +02:00
if not renderer :
renderer = DonRenderer
2018-02-15 18:28:01 +01:00
return renderer ( code ) . render ( size , twoColor , opacity , penwidth )