Navigation

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Users
    • Groups
    • Solved
    • Unsolved
    • Search
    1. Home
    2. erik
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Erik van Blokland

    @erik

    admin

    13
    Reputation
    12
    Posts
    737
    Profile views
    2
    Followers
    0
    Following
    Joined Last Online
    Website letterror.com Location The Hague Age 57

    erik Follow
    drafts admin

    Best posts made by erik

    • Drawing colors with colorsys

      0_1515704525803_dot.png

      Python offers a couple of basic color conversion tools in the colorsys module. In this snippet the hsv_to_rgb() function converts hue (the color), saturation and brightness values (all between 0 and 1) to red, green and blue values.

      import colorsys
      
      s = 50
      b = 20
      step = 0
      brig = .5
      sat = 1
      for x in range(s):
          for y in range(s):
              c = colorsys.hsv_to_rgb(step/(s*s), 1, y/s)
              fill(*c)
              rect(x*b, y*b, b, b)
              step+= 1
      saveImage("dot.png")
      
      posted in Code snippets
      erik
      erik
    • Use drawbot to make a Teams background

      If you're spending time in Microsoft Teams and you're bored with the standard backgrounds, you can actually make your own and impress your audience you did not choose a Starwars theme with your python skills!

      This gist has all the moving parts: the right folder, the right size. Pretty sure other video apps can be serviced with similar paths.

      https://gist.github.com/LettError/9ccc7d7137c0a0b61fd98411fd10655b

      posted in Code snippets
      erik
      erik
    • Aha so that's how antialiasing in gif works..

      In case you ever wondered what happens with antialiasing when saving to .gif:

      If you don't draw a solid background, the gif gets a 1-bit mask and that looks very pixely:

      size(100, 100)
      fill(0)
      oval(10, 10, 80, 80)
      saveImage("test_no_background.gif")
      

      test_no_background.gif

      If you do draw a background, the shape is antialised as expected:

      size(100, 100)
      fill(1)
      rect(0,0,width(),height())
      fill(0)
      oval(10, 10, 80, 80)
      saveImage("test_white_background.gif")!
      

      test_white_background.gif

      posted in General Discussion
      erik
      erik
    • RE: Sorting glyphs by "density"

      There may be useful code here:
      https://github.com/LettError/coverage

      posted in General Discussion
      erik
      erik
    • Possible solution to Just's puzzle?

      0_1515837720190_blob.png

      https://twitter.com/justvanrossum/status/952108619159531520

      # a possible solution to Just's puzzle?
      size(1000,1000)
      fill(0)
      dia = 864
      rect(0,0,width(),height())
      for d in range(1, dia):
          d = dia-d
          fill((dia-d)/dia)
          oval(350-.5*d, 750-.5*d, d, d)
          oval(650-.5*d, 250-.5*d, d, d)
      
      posted in Code snippets
      erik
      erik
    • Excellent 3D Monsters

      0_1515706161344_monsterMovie.png

      By skewing text a couple of degrees forwards or backwards you can give the impression of perspective. But it's not very subtle.

      size(1000, 1000)
      
      theText = [u"EXCELLENT", "MONSTER", "MOVIES"]
      font("HelveticaNeue-CondensedBlack")
      s = 164    # fontsize
      lp = -136    # linespacing
      fontSize(s)
      
      translate(122,590)
      
      # these are the skew angles.
      # protip: select value, command + drag = change value
      leftSlant = -27
      rightSlant = 46
      angle = [leftSlant, rightSlant]
      
      # these values are the tracking of the even / odd lines
      leftTrack = 105 *0.1
      rightTrack = 40 *0.1
      tracks = [leftTrack, rightTrack]
      
      def ip(f, a, b):
          return a+f*(b-a)
      
      for i, theLine in enumerate(theText):    
          wordWidth, textHeight = textSize(theLine )
          sofarWidth = 0
          thisTrack = tracks[i%2]
          for c in theLine:
              thisAngle = angle[i%2]
              letterWidth, textHeight = textSize(c )
              factor = (len(theLine)*thisTrack+sofarWidth+.5*letterWidth)/wordWidth
              save()
              # some alternative skew transforms
              skew(ip(factor, -thisAngle, thisAngle), 0)
              text(c, (0, 0))
              restore()
              translate(letterWidth+thisTrack,0)
              sofarWidth += letterWidth
          translate(-wordWidth-len(theLine)*thisTrack,lp)
      
      saveImage("monsterMovie.png")
      
      posted in Code snippets
      erik
      erik
    • Collinearity of points

      This is an attempt at determining collinearity of a series of points. Rather than compare tangents, one stackoverflower suggested calculating areas of triangles. Other strategies?

      def polygon_area(points):  
          area = 0
          q = points[-1]
          for p in points:  
              area += p[0] * q[1] - p[1] * q[0]
              q = p
          return area / 2
          
      def plot(points):
          if len(points) < 3:
              return None
          a = 0
          for pi, p in enumerate(points[:-2]):
              npt = points[pi+1]
              nnpt = points[pi+2]
              a += abs(polygon_area([points[pi], npt, nnpt]))
          return a
              
      pts = [
          (725,417),
          (548, 440),
          (414, 458),
          (261, 479)
          ]
      
      # tolerance for error
      margin = 200
      
      v = plot(pts)
      
      print("plotted area", v)
      fontSize(100)
      if -.5*margin < v < .5*margin:
          text("colinear",(100,100))
      else:
          text("not colinear",(100,100))
      
      # draw the dots
      fill(0,0,1)
      stroke(1,0,0)
      translate(100,100)
      newPath()
      moveTo(pts[0])
      for p in pts[1:]:
          lineTo(p)
      drawPath()
      
      ds = 10
      fill(0,0,1)
      stroke(None)
      for p in pts:
          oval(p[0]-.5*ds, p[1]-.5*ds, ds, ds)
      
      posted in Code snippets
      erik
      erik

    Latest posts made by erik

    • Use drawbot to make a Teams background

      If you're spending time in Microsoft Teams and you're bored with the standard backgrounds, you can actually make your own and impress your audience you did not choose a Starwars theme with your python skills!

      This gist has all the moving parts: the right folder, the right size. Pretty sure other video apps can be serviced with similar paths.

      https://gist.github.com/LettError/9ccc7d7137c0a0b61fd98411fd10655b

      posted in Code snippets
      erik
      erik
    • Aha so that's how antialiasing in gif works..

      In case you ever wondered what happens with antialiasing when saving to .gif:

      If you don't draw a solid background, the gif gets a 1-bit mask and that looks very pixely:

      size(100, 100)
      fill(0)
      oval(10, 10, 80, 80)
      saveImage("test_no_background.gif")
      

      test_no_background.gif

      If you do draw a background, the shape is antialised as expected:

      size(100, 100)
      fill(1)
      rect(0,0,width(),height())
      fill(0)
      oval(10, 10, 80, 80)
      saveImage("test_white_background.gif")!
      

      test_white_background.gif

      posted in General Discussion
      erik
      erik
    • RE: Sorting glyphs by "density"

      There may be useful code here:
      https://github.com/LettError/coverage

      posted in General Discussion
      erik
      erik
    • fontVariations in BezierPath or formattedString

      Is there a way to set fontVariations values in BezierPath and formattedString?

      posted in General Discussion
      erik
      erik
    • Setting fallback font for text in svg?

      I use drawbot to make svg illustrations. Razorsharp diagrams! For this it would be useful to be able to set a fontstack rather than just a single name.

      Drawbot:
      font('Menlo-Regular')

      SVG:
      font-family="Menlo-Regular"

      Maybe we could add an attribute to the font function that accepts a list of names. For normal non-svg output this could be ignored.

      Drawbot:
      font('Menlo-Regular', fallback=['godforbid-arial', 'sans-serif'])

      SVG:
      font-family="Menlo-Regular, godforbid-arial, sans-serif"

      I can easily open the svg file and manipulate the text to include the extra names. But maybe it is useful to have a more explicit way.

      posted in Feature Requests
      erik
      erik
    • Interpolating Colorbars

      A toy for playing around with color bars with interpolating colors. Currently also prints rgba colors in sass syntax (or at least I think it was sass)

      import colorsys
      
      # calculate color bars with interpolated color steps
      
      size(1000,1000)
      Variable([
          dict(name="steps", ui="Slider", args=dict(value=4, minValue=3, maxValue=15)),
          dict(name="hue", ui="Slider", args=dict(value=0.85, minValue=0, maxValue=1)),
          dict(name="saturation", ui="Slider", args=dict(value=0.6, minValue=0, maxValue=1)),
          dict(name="dark_top", ui="Slider", args=dict(value=.85, minValue=0, maxValue=1)),
          dict(name="dark_bottom", ui="Slider", args=dict(value=.15, minValue=0, maxValue=1)),
          dict(name="offset", ui="Slider", args=dict(value=.33, minValue=-.5, maxValue=.5)),
          ], globals())
      steps = int(steps)
      a = colorsys.hsv_to_rgb(hue%1, saturation, dark_bottom)
      b = colorsys.hsv_to_rgb((hue+offset)%1, saturation, dark_top)
      
      def ip(a,b,f):
          return a+f*(b-a)
      
      h = height()/(steps+1)
      fontSize(min(29, .5*h))
      
      for i in range(steps+1):
          f = i/steps
          c = (ip(a[0],b[0],f), ip(a[1],b[1],f), ip(a[2],b[2],f))
          v = tuple([int(v*255) for v in c])
          fill(*c)
          rect(0, 0, width(), h)
          fill(1,1,1)
          t = "$color%d: rgba%s; /*%3.3f%%*/" %(i, str(v), f*100)
          text(t, (100, .33*h))
          translate(0, h)
          print(t)
      
      saveImage("calculateColorScale.png")
      

      calculateColorScale.png

      posted in Code snippets
      erik
      erik
    • Collinearity of points

      This is an attempt at determining collinearity of a series of points. Rather than compare tangents, one stackoverflower suggested calculating areas of triangles. Other strategies?

      def polygon_area(points):  
          area = 0
          q = points[-1]
          for p in points:  
              area += p[0] * q[1] - p[1] * q[0]
              q = p
          return area / 2
          
      def plot(points):
          if len(points) < 3:
              return None
          a = 0
          for pi, p in enumerate(points[:-2]):
              npt = points[pi+1]
              nnpt = points[pi+2]
              a += abs(polygon_area([points[pi], npt, nnpt]))
          return a
              
      pts = [
          (725,417),
          (548, 440),
          (414, 458),
          (261, 479)
          ]
      
      # tolerance for error
      margin = 200
      
      v = plot(pts)
      
      print("plotted area", v)
      fontSize(100)
      if -.5*margin < v < .5*margin:
          text("colinear",(100,100))
      else:
          text("not colinear",(100,100))
      
      # draw the dots
      fill(0,0,1)
      stroke(1,0,0)
      translate(100,100)
      newPath()
      moveTo(pts[0])
      for p in pts[1:]:
          lineTo(p)
      drawPath()
      
      ds = 10
      fill(0,0,1)
      stroke(None)
      for p in pts:
          oval(p[0]-.5*ds, p[1]-.5*ds, ds, ds)
      
      posted in Code snippets
      erik
      erik
    • image Object should have imagePixelColor method

      The imagePixelColor() function requires a path in order to get a pixel color. Sometimes we already have an image object open. It would be convenient to be able to quiz it for its pixel colors without having to open the file each time.

      posted in Feature Requests
      erik
      erik
    • Define a named color?

      A way to define named colors that, when exported to a pdf, would be recognised as a spot color when imported in InDesign (or similar tools).

      posted in Feature Requests
      erik
      erik
    • Possible solution to Just's puzzle?

      0_1515837720190_blob.png

      https://twitter.com/justvanrossum/status/952108619159531520

      # a possible solution to Just's puzzle?
      size(1000,1000)
      fill(0)
      dia = 864
      rect(0,0,width(),height())
      for d in range(1, dia):
          d = dia-d
          fill((dia-d)/dia)
          oval(350-.5*d, 750-.5*d, d, d)
          oval(650-.5*d, 250-.5*d, d, d)
      
      posted in Code snippets
      erik
      erik