Navigation

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

    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