"""Original cupboard elf, roast dinner, hinged doors and runaway penguins.
Uses the application's existing vector drawing and fixed native palette.
"""
import math
from PIL import Image

def cupboard_assets(Art,dog):
 assets=[];groups={}
 def group(name,images):
  groups[name]=len(assets)
  assets.extend((name+str(i),im) for i,im in enumerate(images))
 def interior():
  a=Art(126,62,'#32271e')
  a.rect((2,2,123,59),'#513c2b')
  a.rect((4,4,121,29),'#3c2b20');a.rect((4,35,121,57),'#34271f')
  a.rect((3,32,123,36),'#a17950');a.line([(4,32),(122,32)],'#c79a67',1)
  a.rect((62,1,65,61),'#896242');a.line([(62,2),(62,59)],'#b08760',.6)
  # Plate, roast turkey with drumsticks, roast potatoes and vegetables.
  a.ellipse((76,26,120,33),'#d7d2b7');a.ellipse((79,27,117,31),'#a9b4a2')
  a.ellipse((85,16,110,30),'#986033');a.ellipse((87,16,108,25),'#c28a4d')
  a.arc((90,17,106,25),195,325,'#e7b568',1)
  a.ellipse((81,23,94,30),'#ae7541');a.line([(84,27),(79,28)],'#e5d4ae',2)
  a.ellipse((103,22,114,29),'#a96b38');a.line([(111,26),(116,24)],'#e5d4ae',2)
  for x,y in [(80,29),(88,30),(113,29)]:a.ellipse((x-3,y-2,x+3,y+1),'#d3a754')
  for x,y in [(80,23),(116,28),(105,30)]:a.ellipse((x-2,y-2,x+2,y+1),'#79985c')
  for x in (93,96,99):a.line([(x,29),(x+2,31)],'#cf8348',1.4)
  a.rect((9,45,31,57),'#47605e',2);a.line([(11,47),(29,47)],'#8ba49a',1)
  a.ellipse((86,48,113,57),'#7c6b52');a.arc((92,43,107,51),180,360,'#aa9a78',2)
  return a.image()
 def elf(frame):
  # Origin is (460,279); the elf sits on the left middle shelf at y=311.
  a=Art(50,58);turn=min(1,frame/7);blush=max(0,(frame-6)/5)
  a.ellipse((16,25,33,36),'#4b7957');a.rect((19,26,29,38),'#3a6c4f',3)
  a.rect((17,33,33,36),'#5e4132');a.rect((23,33,27,36),'#d7b66b')
  for x in (19,29):
   a.line([(x,37),(x-1,46)],'#c54f51',3)
   for y in (39,43):a.line([(x-2,y),(x+1,y)],'#e7d3aa',1)
   a.ellipse((x-5,44,x+3,48),'#3b664c')
  cx=26-3*turn
  a.poly([(cx-8,13),(cx-14,11),(cx-9,20)],'#d8b185')
  a.poly([(cx+7,13),(cx+13,11),(cx+8,20)],'#d8b185')
  a.ellipse((cx-9,9,cx+9,27),'#e7c398')
  a.poly([(cx-9,10),(cx-3,1),(cx+3,3),(cx+10,8),(cx+13,6),(cx+9,12)],'#b3484e')
  a.line([(cx-9,11),(cx+9,11)],'#e5d8b9',2)
  a.ellipse((cx+11,4,cx+15,8),'#e9d9af')
  if turn<.35:
   a.ellipse((cx+4,16,cx+6,18),'#3b3027')
   a.poly([(cx+7,16),(cx+12,19),(cx+7,21)],'#e7c398')
   a.arc((cx+3,19,cx+8,24),0,110,'#8a5540',.7)
  else:
   for x in (cx-4,cx+4):
    a.ellipse((x-1,16,x+1,18),'#3b3027')
    a.line([(x-2,14),(x+1,13 if blush>.2 else 14)],'#82563c',.7)
   a.ellipse((cx-1.5,19,cx+1.5,21),'#d6a47d')
   a.arc((cx-3,22,cx+3,25),190,335,'#8c5746',.7)
  if blush>0:
   colour=tuple(round(u+(v-u)*min(1,blush)) for u,v in zip((220,163,128),(224,99,100)))
   for x in (cx-6,cx+6):a.ellipse((x-2.4,20,x+2.4,23.5),colour)
  for x in (15,34):
   a.line([(22 if x<20 else 28,28),(x,32)],'#50835a',3)
   a.ellipse((x-2,30,x+2,34),'#e6bf91')
  return a.image()
 def doors(frame,pull=False):
  # Two vertical outer hinges; inner edges swing towards the viewer.
  a=Art(166,86);theta=frame/20*math.radians(111)
  co=math.cos(theta);depth=12*math.sin(theta)
  if pull:
   # Arms recede behind the closing door panels; only the mittens grip
   # the visible knobs. Never paint an arm through the solid wood.
   for side,hinge,width in [(1,22,62),(-1,144,60)]:
    knobx=hinge+side*(width-8)*co;knoby=31+depth*(width-8)/width
    shoulder=(49 if side==1 else 57,31)
    a.line([shoulder,((shoulder[0]+knobx)/2,knoby+5),(knobx,knoby)],'#4d7953',3)
  for side,hinge,width in [(1,22,62),(-1,144,60)]:
   edge=hinge+side*width*co
   top=4;bottom=62
   points=[(hinge,top),(edge,top+depth),(edge,bottom+depth),(hinge,bottom)]
   a.poly(points,'#906a49' if co>=0 else '#78573d')
   a.line(points+[points[0]],'#b18a61',1.2)
   if abs(co)>.16:
    inset=side*4*co
    a.line([(hinge+inset,top+4),(edge-inset,top+depth+4),(edge-inset,bottom+depth-4),(hinge+inset,bottom-4),(hinge+inset,top+4)],'#7d5c41',.7)
   knobx=hinge+side*(width-8)*co;knoby=31+depth*(width-8)/width
   a.ellipse((knobx-1,knoby-1,knobx+1,knoby+1),'#d4ba87')
   if pull:
    a.ellipse((knobx-2,knoby-2,knobx+2,knoby+2),'#e7c398')
  return a.image()
 def penguin(kind,frame,left):
  a=Art(28,36);air=frame==8;phase=frame*math.tau/8
  bounce=0 if air else abs(math.sin(phase))*1.2
  # Feet use a fixed bottom-centre anchor for jumps and surface landings.
  for side in (-1,1):
   dx=side*(3 if air else 3+2*math.sin(phase));fy=33-(2 if air else max(0,side*math.cos(phase))*2)
   a.ellipse((14+dx-3,fy-1,14+dx+4,fy+1),'#db9b46')
  a.ellipse((6,11-bounce,23,32-bounce),'#26333b')
  a.ellipse((9,17-bounce,22,31-bounce),'#f1e9d1')
  flap=-6 if air else math.sin(phase)*3
  a.poly([(7,17-bounce),(1,22+flap-bounce),(3,26+flap-bounce),(10,23-bounce)],'#33444c')
  a.ellipse((9,5-bounce,24,20-bounce),'#26333b')
  a.ellipse((16,9-bounce,23,17-bounce),'#f1e9d1')
  a.ellipse((20,10-bounce,22,12-bounce),'#24252b')
  a.poly([(22,13-bounce),(27,15-bounce),(22,17-bounce)],'#e6a84e')
  colour=['#bd4d57','#6ba2ae','#d7af65','#70956e','#a17399'][kind]
  a.line([(8,19-bounce),(22,19-bounce)],colour,3)
  a.poly([(9,20-bounce),(4,22-bounce),(2,28-bounce),(7,26-bounce)],colour)
  a.poly([(11,6-bounce),(17,0),(24,5-bounce)],'#af424c')
  a.line([(10,7-bounce),(24,7-bounce)],'#eddfbd',2)
  a.ellipse((15,-1,19,3),'#f0dfbb')
  im=a.image()
  return im.transpose(Image.Transpose.FLIP_LEFT_RIGHT) if left else im
 def glare(frame):
  p=frame/8;a=Art(102,75);dy=16
  a.ellipse((20,12+dy,91,48+dy),'#8c6045');a.ellipse((29,14+dy,86,45+dy),'#ad8057')
  a.ellipse((58,29+dy,95,49+dy),'#967049');a.arc((51,25+dy,95,49+dy),330,200,'#c29b6c',5)
  hy=dy-10*p
  for box,col in [((11,21,43,46),'#ba9063'),((10,20,24,43),'#694b3b'),((33,21,45,43),'#71513c'),((20,36,44,49),'#d2b38a'),((30,37,38,42),'#3e3530')]:
   a.ellipse((box[0],box[1]+hy,box[2],box[3]+hy),col)
  for x in (26,37):
   if p>.2:
    a.ellipse((x-3,30+hy,x+3,34+hy),'#ddc7a4')
    a.ellipse((x-.8,29.5+hy,x+1.2,32+hy),'#493b2b')
    a.line([(x-3.5,30+hy),(x+3,31+hy)],'#5f4935',1.3)
   else:a.line([(x-3,32+hy),(x,33+hy),(x+3,32+hy)],'#43372e',1)
  a.ellipse((40,42+dy,62,49+dy),'#bc976c');a.ellipse((44,45+dy,66,51+dy),'#c4a279')
  return a.image()
 group('cupinside',[interior()])
 group('cupelf',[elf(i) for i in range(12)])
 group('cupdoor',[doors(i) for i in range(21)])
 group('cuppull',[doors(i,True) for i in range(21)])
 group('cuppeng',[penguin(k,n,left) for k in range(5) for left in range(2) for n in range(9)])
 group('dogglare',[glare(i) for i in range(9)])
 return assets,groups
