/*
 * Copyright © 2005 Keith Packard
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Keith Packard
 *
 * Contributor(s):
 *      Keith Packard <keithp@keithp.com>
 */

/*
 * Generate LED images for rocketview and launchcontrol
 */
 
autoimport Cairo; 

void led (cairo_t cr, real red, real green, real blue, bool on)
{
    translate (cr, .5, .5);
    
    /* basic unilluminated LED */
    
    if (true)
    {
	set_source_rgb (cr, red *.4, green *.4, blue *.4);
	move_to (cr, 1, 1/2);
	arc (cr, .5, .5, .5, 0, pi*2);
	fill (cr);
    }
    
    /* Bright spot of LED illumination */
    if (on)
    {
	pattern_t	led = Pattern::create_radial (1/2, 1/2, 0, 
						      1/2, 1/2, .4);
	Pattern::add_color_stop_rgba (led, 0, red, green, blue, 1);
	Pattern::add_color_stop_rgba (led, .5, red, green, blue, .8);
	Pattern::add_color_stop_rgba (led, 1, red, green, blue, 0);
	set_source (cr, led);
	move_to (cr, 1, 1/2);
	arc (cr, .5, .5, .5, 0, pi*2);
	fill (cr);
    }
	
    /* Bezel */
    if (true)
    {
	pattern_t	ring = Pattern::create_radial (.5, .5, .4, .5, .5, .5);
	Pattern::add_color_stop_rgba (ring, 0, 0, 0, 0, 1);
	Pattern::add_color_stop_rgba (ring, .5, 0, 0, 0, 1);
	Pattern::add_color_stop_rgba (ring, 1, .5, .5, .5, 1);
	set_source (cr, ring);
	move_to (cr, 1, 1/2);
	arc (cr, .5, .5, .5, 0, pi*2);
	move_to (cr, 1, 1/2);
	arc_negative (cr, .5, .5, .45, pi*2, 0);
	fill (cr);
    }

    /* Specular highlight from room illumination */
    if (true)
    {
	pattern_t	room = Pattern::create_radial (1/3, 1/3, 0, 1/3, 1/3, 1/2);
	Pattern::add_color_stop_rgba (room, 0, 1, 1, 1, .4);
	Pattern::add_color_stop_rgba (room, 1, 1, 1, 1, 0);
	set_source (cr, room);
	move_to (cr, 1, 1/2);
	arc (cr, .5, .5, .5, 0, pi*2);
	fill (cr);
    }

}

/*
 * Desired LED image size in pixels
 */
int diameter = 12;

void do_one (string name, real red, real green, real blue, bool on)
{
    cairo_t cr = new_image (diameter, diameter);
    
    translate (cr, -width(cr)/2, -height(cr)/2);
    scale (cr, width(cr), height(cr));
    led (cr, red, green, blue, on);
    write_to_png (cr, name);
    destroy (cr);
}

void doit ()
{
    do_one ("redled.png", 1, 0, 0, true);
    do_one ("redoff.png", 1, 0, 0, false);
    do_one ("greenled.png", 0, 1, .4824, true);
    do_one ("greenoff.png", 0, 1, .4824, false);
    do_one ("grayled.png", .9, .9, .9, false);
    do_one ("grayon.png", .9, .9, .9, true);
}

if (dim(argv) > 0)
    doit ();
