Skip to content

Instantly share code, notes, and snippets.

@sheerun
Last active September 29, 2019 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheerun/bfa1a7ec3447442f8a366db51ef36da6 to your computer and use it in GitHub Desktop.
Save sheerun/bfa1a7ec3447442f8a366db51ef36da6 to your computer and use it in GitHub Desktop.
Script for drawing on http://editfight.com/
// Compile with:
// gcc -o click click.m -framework ApplicationServices -framework Foundation
//
// Usage:
// ./click -x pixels -y pixels
// At the given coordinates it will click and release.
//
// From http://hints.macworld.com/article.php?story=2008051406323031
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
int x = [args integerForKey:@"x"];
int y = [args integerForKey:@"y"];
CGPoint pt;
pt.x = x;
pt.y = y;
CGPostMouseEvent( pt, 1, 1, 1 );
CGPostMouseEvent( pt, 1, 1, 0 );
[pool release];
return 0;
}
# I tested this on OSX.
# You need to install dependencies first:
#
# brew install imagemagick@6
# PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig gem install rmagick
# gem install color_diff
# You also need to download color palette from
# curl -o full.png https://i.imgur.com/H9Qpc4J.png
#
# Now setup the browser so top left pixel is at [52, 173]
# and the height of browser is as such whole board is 500x500 pixels.
#
# Usage: ruby draw.rb
#
# If you find this fun, please follow me on twitter :)
# https://twitter.com/sheerun
#
require 'rubygems'
require 'rmagick'
require 'color_diff'
def rgb(r, g, b)
{ r: r.to_f / 255, g: g.to_f / 255, b: b.to_f / 255 }
end
$colors = [
rgb(0, 0, 0),
rgb(87, 87, 87),
rgb(173, 35, 35),
rgb(42, 75, 215),
rgb(29, 105, 20),
rgb(129, 74, 25),
rgb(129, 38, 192),
rgb(160, 160, 160),
rgb(129, 197, 122),
rgb(157, 175, 255),
rgb(41, 208, 208),
rgb(255, 146, 51),
rgb(255, 238, 51),
rgb(233, 222, 187),
rgb(255, 205, 243),
rgb(255, 255, 255)
]
def click(x, y)
system("click -x #{x} -y #{y}")
end
def color(index)
click(73+40*index, 146)
end
def color_distance(c1, c2)
ColorDiff.between(
ColorDiff::Color::RGB.new(c1[:r], c1[:g], c1[:b]),
ColorDiff::Color::RGB.new(c2[:r], c2[:g], c2[:b]),
)
end
def closest_color(color)
_, index = $colors.each_with_index.min_by do |c, i|
color_distance(color, c)
end
index
end
def pixel_at(img, x, y)
width = img.columns
height = img.rows
color = img.pixel_color(x, y)
rgb(color.red / 257, color.green / 257, color.blue / 257)
end
def color_at(img, x, y)
width = img.columns
height = img.rows
color = img.pixel_color((width * x).round, (height * y).round)
rgb(color.red / 257, color.green / 257, color.blue / 257)
end
def rectangle(startx, starty, width, height, col)
i = 0
x0 = 52
y0 = 173
color(col)
click(x0, y0)
(startx...startx+width).each do |x|
(starty...starty+height).each do |y|
click(x0 + 5*x, y0+5*y)
end
end
end
$palette = Magick::Image::read("full.png")[0]
def draw(startx, starty, width, height, img)
i = 0
x0 = 52
y0 = 173
if height == "auto"
height = ((img.rows.to_f / img.columns) * width).round
end
if width == "auto"
width = ((img.columns.to_f / img.rows) * height).round
end
img = img.remap($palette, Magick::NoDitherMethod)
img = img.resize_to_fill(width, height)
click(x0, y0)
current_color = 0
(0...width).each do |x|
(0...height).each do |y|
next_color = closest_color(pixel_at(img, x, y))
if next_color != current_color
color(next_color)
current_color = next_color
end
click(x0+5*(startx+x), y0+5*(starty+y))
end
end
end
color(0); color(0)
img = Magick::Image::read("/Users/sheerun/Desktop/southpark.png")[0]
draw(0, 0, 20, "auto", img)
@sdegutis
Copy link

The color palette is missing color 15 (16th) though?

@sheerun
Copy link
Author

sheerun commented Apr 14, 2018

@sdegutis white is background

@sdegutis
Copy link

Oh right makes sense. So you just omit it then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment