1308: "Christmas Lights"

This forum is for the individual discussion thread that goes with each new comic.

Moderators: Moderators General, Prelates, Magistrates

Kanawha
Posts: 1
Joined: Fri Dec 27, 2013 6:20 pm UTC

Re: 1308: "Christmas Lights"

Postby Kanawha » Mon Dec 30, 2013 1:01 pm UTC

There must be a blue star on top.

George2
Posts: 12
Joined: Sun Jul 13, 2008 3:41 pm UTC
Location: Egypt

Re: 1308: "Christmas Lights"

Postby George2 » Wed Jan 01, 2014 2:26 am UTC

christmas_lights-2.png

Spoiler:

Code: Select all

#Python 2.5

#open original comic in ms paint
#pour pink (#ff0080) in the empty space
#save as 24-bit bitmap with the name "christmas_lights.bmp"

a=file('christmas_lights.bmp','rb+'); header=a.read(54)

pixels=[['']*554 for y in range(282)]

nonbgd=[]

for y in range(282):
 for x in range(554):
  pixcol=a.read(3); pixels[y][x]=pixcol
  if pixcol=='\x80\x00\xff' or (x,y) in [(74,31),(75,31),(464,62),(464,63)]: pass
  else: nonbgd.append((x,y))
 k=a.read(2)
a.close()

nonbgd=set(nonbgd)

def groupof(x,y):
 edges=set([(x,y)]); group=edges
 while edges:
  newedges=reduce(lambda x,y: x+y, [[(px-1,py),(px+1,py),(px,py-1),(px,py+1)] for (px,py) in edges])
  newedges=(set(newedges)-group)&nonbgd
  group=group|newedges; edges=newedges
 return group

groups=[]
while nonbgd:
 curgroup=groupof(*nonbgd.pop())
 nonbgd-=curgroup
 if set([(0,0),(47,80),(369,86)])&curgroup: pass
 else: groups.append(curgroup)

def color(group):
 group=list(group); group.sort(); i=0; r,g,b=0,0,0; maxy=0
 while i<len(group):
  curx=group[i][0]; curcol=[]; miny=group[i][1]
  while i<len(group) and group[i][0]==curx: curcol.append(pixels[group[i][1]][group[i][0]]); i+=1
  colheight=group[i-1][1]-miny
  if group[i-1][1]>maxy: maxy=group[i-1][1]
  try:
   b2,g2,r2 = [ord(c) for c in curcol[3]]
   r+=r2*colheight; g+=g2*colheight; b+=b2*colheight
  except: pass
 maxc=max(r,g,b)
 miny=min(group, key=lambda x: x[1])[1]
 color=(r*256/maxc,g*256/maxc,b*256/maxc)
 center=((group[0][0]+group[-1][0])/2.,(miny+maxy)/2.)
 size=((group[-1][0]-group[0][0])*(maxy-miny))**0.5
 print ((group[-1][0]-group[0][0]),(maxy-miny)),maxy,group[0][1]
 return color,center,size


outfile=file('lights.html','wb+')
outfile.write('<!DOCTYPE html>\r\n<html>\r\n<body>\r\n<svg width="554" height="282">\r\n<rect width="554" height="282" style="fill:black"/>\r\n<defs>\r\n<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">\r\n<stop offset="65%" style="stop-color:black;stop-opacity:0"/>\r\n<stop offset="100%" style="stop-color:black;stop-opacity:.9"/>\r\n</radialGradient>\r\n</defs>\r\n')
for group in groups:
 k=color(group)
 outfile.write('<circle cx="%s" cy="%s" r="%s" fill="rgb%s"/>\r\n'%(k[1][0],281-k[1][1],k[2]/2,k[0]))
 outfile.write('<circle cx="%s" cy="%s" r="%s" fill="url(#grad1)"/>\r\n'%(k[1][0],281-k[1][1],k[2]*1.01/2))
outfile.write('</svg>\r\n</body>\r\n</html>')

I was bored.

Happy new year everyone :D

User avatar
Flumble
Yes Man
Posts: 2235
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: 1308: "Christmas Lights"

Postby Flumble » Wed Jan 01, 2014 11:48 pm UTC

Darn, I ought to do exactly that, only in javascript. Congratulations on beating me to it and a happy new year. :lol:


Return to “Individual XKCD Comic Threads”

Who is online

Users browsing this forum: Leovan, Steeler [Crawler] and 35 guests