Skip to content

Library of Functions for Scripts

haloflooder edited this page Aug 31, 2018 · 3 revisions

I made some functions that are useful for Aseprite. Currently, the functions cannot be imported to a script (for now) so you have to copy/paste the code to the script. The functions are made in javascript and not LUA (for now)

List of Functions:

HSV to RGB Converter

This function converts HSV values to RGB values. This is useful for analyzing colors or whatever else you might need it for.

Show Code Snippet
function hsv2rgb(h,s,v) {
	var r, g, b, i, f, p, q, t;
	
	h = h/255;
	s = s/255;
	v = v/255;
	
	i = Math.floor(h * 6);
	f = h * 6 - i;
	p = v * (1 - s);
	q = v * (1 - f * s);
	t = v * (1 - (1 - f) * s);
	switch (i % 6) {
		case 0: r = v, g = t, b = p; break;
		case 1: r = q, g = v, b = p; break;
		case 2: r = p, g = v, b = t; break;
		case 3: r = p, g = q, b = v; break;
		case 4: r = t, g = p, b = v; break;
		case 5: r = v, g = p, b = q; break;
	}
	return [
		Math.round(r * 255),
		Math.round(g * 255),
		Math.round(b * 255)
	];
}
Show Example
pc = app.PixelColor;
img = app.activeImage;

var hue = 0;
var sat = 255;
var val = 255;
for (var y=0; y<img.height; ++y) {
	for (var x=0; x<img.width; ++x) {
		var hsv = hsv2rgb(hue%255,sat,val); // This helps us convert numbers into a pretty rainbow
		var c = pc.rgba(hsv[0],hsv[1],hsv[2],255);
		img.putPixel(x,y,c);
		hue++;
	}
}

RGB to HSV Converter

This function converts RGB values to HSV values. This was primarily used to make rainbow colors easily by just changing the hue value. You could also use it for anything else you might need it for as well.

Show Code Snippet
function rgb2hsv(r, g, b) { 
	r /= 255, g /= 255, b /= 255;

	var max = Math.max(r, g, b), min = Math.min(r, g, b);
	var h, s, v = max;

	var d = max - min;
	s = max == 0 ? 0 : d / max;

	if (max == min) {
		h = 0;
	} else {
		switch (max) {
			case r: h = (g - b) / d + (g < b ? 6 : 0); break;
			case g: h = (b - r) / d + 2; break;
			case b: h = (r - g) / d + 4; break;
		}
		h /= 6;
	}

	return [ 
		h * 255, 
		s * 255, 
		v * 255
	];
}
Show Example
pc = app.PixelColor;
img = app.activeImage;

for (var y=0; y<img.height; ++y) {
	for (var x=0; x<img.width; ++x) {
		var c = img.getPixel(x,y)
		var r = pc.rgbaR(c);
		var g = pc.rgbaG(c);
		var b = pc.rgbaB(c);
		var hsv = rgb2hsv(r,g,b);
		// Do whatever you want here
		// hsv[0] = hue
		// hsv[1] = sat
		// hsv[2] = val
	}
}

Text Spacer (For making cleaner console outputs)

This function helps with the creation of cleaner console outputs. I need to make a more user friendly version of this function so it's easier to use.

Show Code Snippet
to-do
Show Example
to-do