Mailing List Archive

my python progress
i've had some fun recently when i've had a chance to work
on it. i needed some kind of project that would encourage
me to learn more python. i still consider myself very new
to the language and a long ways to go (and i don't consider
the code as a great example, but it is progress to me in my
own understanding of python).

pyglet the opengl graphics modules have been providing some
fun too.


here is my recent update to this project:

https://github.com/flowerbug/npath

which took the following code and then changes it into a more
general framework and then as a practical result i was able to use
this framework to create and test the first version of the class
(posted at the bottom).

https://github.com/flowerbug/ngfp


one thing i would like to have is a version of the GIMP
button border code converted from the GIMP version of lisp
to python. i used to be fluent enough in lisp that i can
actually figure out most of what the code does but i am
not familiar enough with GIMP and graphics to fully understand
the code to be able to convert it. if you are up for the
challenge i would appreciate it.


=====
; GIMP - The GNU Image Manipulation Program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
;
; Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
;
; Version 0.2 10.6.97 Changed to new script-fu interface in 0.99.10

; Delta the color by the given amount. Check for boundary conditions
; If < 0 set to zero
; If > 255 set to 255
; Return the new value

(define (script-fu-addborder aimg adraw xsize ysize color dvalue)

(define (deltacolor col delta)
(let* ((newcol (+ col delta)))
(if (< newcol 0) (set! newcol 0))
(if (> newcol 255) (set! newcol 255))
newcol
)
)

(define (adjcolor col delta)
(mapcar (lambda (x) (deltacolor x delta)) col)
)

(define (gen_top_array xsize ysize owidth oheight width height)
(let* ((n_array (cons-array 10 'double)))
(aset n_array 0 0 )
(aset n_array 1 0 )
(aset n_array 2 xsize)
(aset n_array 3 ysize)
(aset n_array 4 (+ xsize owidth))
(aset n_array 5 ysize)
(aset n_array 6 width)
(aset n_array 7 0 )
(aset n_array 8 0 )
(aset n_array 9 0 )
n_array)
)

(define (gen_left_array xsize ysize owidth oheight width height)
(let* ((n_array (cons-array 10 'double)))
(aset n_array 0 0 )
(aset n_array 1 0 )
(aset n_array 2 xsize)
(aset n_array 3 ysize)
(aset n_array 4 xsize)
(aset n_array 5 (+ ysize oheight))
(aset n_array 6 0 )
(aset n_array 7 height )
(aset n_array 8 0 )
(aset n_array 9 0 )
n_array)
)

(define (gen_right_array xsize ysize owidth oheight width height)
(let* ((n_array (cons-array 10 'double)))
(aset n_array 0 width )
(aset n_array 1 0 )
(aset n_array 2 (+ xsize owidth))
(aset n_array 3 ysize)
(aset n_array 4 (+ xsize owidth))
(aset n_array 5 (+ ysize oheight))
(aset n_array 6 width)
(aset n_array 7 height)
(aset n_array 8 width )
(aset n_array 9 0 )
n_array)
)

(define (gen_bottom_array xsize ysize owidth oheight width height)
(let* ((n_array (cons-array 10 'double)))
(aset n_array 0 0 )
(aset n_array 1 height)
(aset n_array 2 xsize)
(aset n_array 3 (+ ysize oheight))
(aset n_array 4 (+ xsize owidth))
(aset n_array 5 (+ ysize oheight))
(aset n_array 6 width)
(aset n_array 7 height)
(aset n_array 8 0 )
(aset n_array 9 height)
n_array)
)

(let* ((img (car (gimp-item-get-image adraw)))
(owidth (car (gimp-image-get-width img)))
(oheight (car (gimp-image-get-height img)))
(width (+ owidth (* 2 xsize)))
(height (+ oheight (* 2 ysize)))
(layer (car (gimp-layer-new img
width height
(car (gimp-drawable-type-with-alpha adraw))
_"Border Layer" 100 LAYER-MODE-NORMAL))))

(gimp-context-push)
(gimp-context-set-paint-mode LAYER-MODE-NORMAL)
(gimp-context-set-opacity 100.0)
(gimp-context-set-antialias FALSE)
(gimp-context-set-feather FALSE)

(gimp-image-undo-group-start img)

(gimp-image-resize img
width
height
xsize
ysize)

(gimp-image-insert-layer img layer 0 0)
(gimp-drawable-fill layer FILL-TRANSPARENT)

(gimp-context-set-background (adjcolor color dvalue))
(gimp-image-select-polygon img
CHANNEL-OP-REPLACE
10
(gen_top_array xsize ysize owidth oheight width height))
(gimp-drawable-edit-fill layer FILL-BACKGROUND)
(gimp-context-set-background (adjcolor color (/ dvalue 2)))
(gimp-image-select-polygon img
CHANNEL-OP-REPLACE
10
(gen_left_array xsize ysize owidth oheight width height))
(gimp-drawable-edit-fill layer FILL-BACKGROUND)
(gimp-context-set-background (adjcolor color (- 0 (/ dvalue 2))))
(gimp-image-select-polygon img
CHANNEL-OP-REPLACE
10
(gen_right_array xsize ysize owidth oheight width height))

(gimp-drawable-edit-fill layer FILL-BACKGROUND)
(gimp-context-set-background (adjcolor color (- 0 dvalue)))
(gimp-image-select-polygon img
CHANNEL-OP-REPLACE
10
(gen_bottom_array xsize ysize owidth oheight width height))

(gimp-drawable-edit-fill layer FILL-BACKGROUND)
(gimp-selection-none img)
(gimp-image-undo-group-end img)
(gimp-displays-flush)

(gimp-context-pop)
)
)

(script-fu-register "script-fu-addborder"
_"Add _Border..."
_"Add a border around an image"
"Andy Thomas <alt@picnic.demon.co.uk>"
"Andy Thomas"
"6/10/97"
"*"
SF-IMAGE "Input image" 0
SF-DRAWABLE "Input drawable" 0
SF-ADJUSTMENT _"Border X size" '(12 1 250 1 10 0 1)
SF-ADJUSTMENT _"Border Y size" '(12 1 250 1 10 0 1)
SF-COLOR _"Border color" '(38 31 207)
SF-ADJUSTMENT _"Delta value on color" '(25 1 255 1 10 0 1)
)

(script-fu-menu-register "script-fu-addborder"
"<Image>/Filters/Decor")

=====


my own simpler version of a bordered button class is below, but
i have not fully tested it yet, but it works for the square version
that i'm using at the moment. i'll have to update my progam a bit
some more to test out the rectangular version (that's my next bit
of challenge).


=====
from math import sqrt

import pyglet
from pyglet.image import SolidColorImagePattern, ImageData


class SolidColorButtonImagePattern(SolidColorImagePattern):
"""Creates a beveled button image filled with a solid color."""

def __init__(self, color=(0, 0, 0, 0), border_color=(0,0,0,255)):
"""Create a beveled image pattern with color and blend the
border towards border_color.

:Parameters:
`color` : (int, int, int, int)
4-tuple of ints in range [0,255] giving RGBA components of
color to fill with.
`border_color` : (int, int, int, int)
4-tuple of ints in range [0,255] giving RGBA components of
the border color to blend towards.

"""
if len(color) != 4:
raise TypeError("color is expected to have 4 components")
self.color = list(color)
self.border_color = list(border_color)

def create_image(self, width, height):

data = self.color * width * height

if (width < 3) or (height < 3):
print ("width or height < 3 : ", width, height, " No Border Applied to image.")
img_data = ImageData(width, height, 'RGBA', bytes(data))
return img_data

x_border = int(width / sqrt(width))
y_border = int(height / sqrt(height))
if (x_border == 0):
x_border = 1
if (y_border == 0):
y_border = 1

# make the borders are uniform make sure they are the same size
# even if the width and height of the tiles are different
if (x_border != y_border):
y_border = (x_border + x_border) // 2
x_border = y_border

# how many gradient steps to use for each part of the color and alpha
step_x_red = (self.border_color[0] - self.color[0]) / x_border
step_y_red = (self.border_color[0] - self.color[0]) / y_border
step_x_green = (self.border_color[1] - self.color[1]) / x_border
step_y_green = (self.border_color[1] - self.color[1]) / y_border
step_x_blue = (self.border_color[2] - self.color[2]) / x_border
step_y_blue = (self.border_color[2] - self.color[2]) / y_border
step_x_alpha = (self.border_color[3] - self.color[3]) / x_border
step_y_alpha = (self.border_color[3] - self.color[3]) / y_border

# bottom and top row(s)
red_x = self.border_color[0]
green_x = self.border_color[1]
blue_x = self.border_color[2]
alpha_x = self.border_color[3]
for x in range(x_border):
for y in range(width-(x*2)):
indx_lower = (x*width*4)+((y+x)*4)
indx_upper = ((height-(x+1))*width*4)+((y+x)*4)
data[indx_lower] = int(red_x)
data[indx_upper] = int(red_x)
data[indx_lower+1] = int(green_x)
data[indx_upper+1] = int(green_x)
data[indx_lower+2] = int(blue_x)
data[indx_upper+2] = int(blue_x)
data[indx_lower+3] = int(alpha_x)
data[indx_upper+3] = int(alpha_x)

red_x -= step_x_red
green_x -= step_x_green
blue_x -= step_x_blue
alpha_x -= step_x_alpha

# left and right col(s)
red_y = self.border_color[0]
green_y = self.border_color[1]
blue_y = self.border_color[2]
alpha_y = self.border_color[3]
for x in range(y_border):
for y in range(height-(x*2)):
indx_left = (((y+x)*height)+(x))*4
indx_right = (((y+x)*height)+(width-x-1))*4
data[indx_left] = int(red_y)
data[indx_right] = int(red_y)
data[indx_left+1] = int(green_y)
data[indx_right+1] = int(green_y)
data[indx_left+2] = int(blue_y)
data[indx_right+2] = int(blue_y)
data[indx_left+3] = int(alpha_y)
data[indx_right+3] = int(alpha_y)

red_y -= step_y_red
green_y -= step_y_green
blue_y -= step_y_blue
alpha_y -= step_y_alpha

img_data = ImageData(width, height, 'RGBA', bytes(data))
return img_data

=====

so never give up hope of teaching an old dog some new tricks. :)


songbird
--
https://mail.python.org/mailman/listinfo/python-list