Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Patchwork quilt pattern in Ruby (from Knuth TAoCP 4 - 7.1.3)

In Donald Knuth's TAoCP Pre-Fascicle 1a: Bitwise Tricks and Techniques, he shows a patchwork quilt defined by f(x, y) = ((x ^ y) & ((y - 350) >> 3)) ** 2, designed by D. Sleator in 1976. I wanted to recreate this quilt myself using Ruby. The following program generates a PNG file of the "quilt."

require 'rubygems'
require 'png'

def f(x, y)
  ( (x ^ y) & ((y - 350) >> 3) ) ** 2
end

canvas = PNG::Canvas.new(500, 500)

0.upto(499) do |y|
  0.upto(499) do |x|
    canvas[x, 499 - y] = ((f(x, y) >> 12) & 1) == 1 ? PNG::Color::Black : PNG::Color::White
  end
end

png = PNG.new(canvas)
png.save 'pattern.png'

dynamic rails img headers

// description of your code here

find views -name [a-z]\*rhtml | xargs -n1 grep -H "@page_title" | grep -v "<%" | sed "s/\@page_title = //" | sed "s/rhtml/png/" | sed "s:views/::" | sed "s:/:_:g" | sed "s:^:public/images/beta/headers/hdr_:" | sed "s/  //g"

PNG-24 Alpha support for IE

What so much webdesigners dream about!
Get alpha channel on web, that's possible with PNG-24 images and this trick.
BE CAREFUL: that seem to work only for 10 images per page.

  # Display PNG-24 images with alpha channel on IE
  # BE CAREFUL: with this trick, only 10 PNGs seems to be supported by IE
  # Don't forget to set the size of your div
  def transpng(id, png)
    '<style type="text/css">
      <!--
        ' + id + ' {
          background-image: url(' + png + ');
        }
      -->
    </style>
    <!--[if IE]>
    <style>
      ' + id + ' {
        background-image: none;
        filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + png + ', sizingMethod=\'scale\');
      }
    </style>
    <![endif]-->'
  end

My default layout in Rails projects

Writing a new layout from scratch gets boring, so I have a generic / default layout to use and extend from.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><%= @page_title %></title>
	<%= stylesheet_link_tag "main" %>
	<%= javascript_include_tag "prototype" %>
	<%= javascript_include_tag "scriptaculous" %>
	<%= javascript_include_tag "general" %>
	<!--[if lt IE 7.]>
	<script defer type="text/javascript" src="/javascripts/pngfix.js"></script>
	<![endif]-->
</head>

<body class="<%= @body_class %>">
	<div id="container">
			
	<%= @content_for_layout %>

	</div>
</body>
</html>


pngfix.js is available here.

Get pixel colors of an image

Pys60 now has a decent Canvas and Image class.
We can draw many shapes and color them.
However, one important feature is missing, ie. getpixel().
So, you can only 'write' but not 'read' from graphics.

To get around this problem, I write a small library that
add 'getpixel' to the Image class.
Now, you can call im.getpixel(x,y) and get an (R,G,B) tuple.
# some setup
from graphics import *
im = screenshot()  # sample image

# http://larndham.net/service/pys60/getpixel.py
import getpixel
getpixel.enable(im)  # magically give Image.getpixel()
r, g, b = im.getpixel(0,0)  # top left corner
print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)

Now you can do some easy image processing with getpixel.

Implementation note
===================
- This is a pure python module
- It saves an image as an uncompressed PNG file
- It reads pixel data from the file and attach it to the image.
- You need to call enable() every time you change the image.

Change OS X's default screenshot image format

At the prompt, type this:

defaults write com.apple.screencapture type image_format


Replace "image_format" with a file format name, like pdf, png, tiff, etc. Then to take screenshots, Cmd+Shift+4, then drag around the area you want to capture.
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS