/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* coca::colorset.js                                                        */
/* 'The DHTML Color Calculator' javascript source                           */
/* Copyright (c) 2002 Sebastian Böthin <boethin@math.fu-berlin.de>          */
/*                                                                          */
/* Permission to use, copy, and distribute for non-commercial purposes,     */
/* is hereby granted without fee, providing that the above copyright        */ 
/* notice appear in all copies and that both the copyright notice and       */
/* this permission notice appear in supporting documentation.               */
/*                                                                          */
/* This software is provided "as is" without any expressed or implied       */
/* warranty. The author shall not be liable for any damages suffered by     */
/* users of this software. USE AT YOUR OWN RISK.                            */
/*                                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* requires                                                                 */
/*   coca::show.js                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

// ---------------------------------------------------------------------------
// ColorStack
// a simple stack

function ColorStack(size) {
    this.size = size;
	this.length = 0;
	this.stptr = 0;
	this.stack = new Array(size);
	for (var i = 0; i < size; i++) this.stack[i] = null;
	this.push = ColorStack_push;
	this.pop = ColorStack_pop;
	this.peek = ColorStack_peek;
	return this;
}

function ColorStack_push(c) {
	this.stack[this.stptr] = c;
	this.stptr = (this.stptr + 1) % this.size;
	this.length++;
}

function ColorStack_pop() {
	if (this.length == 0) return null;
	var ptr = (this.size + this.stptr - 1) % this.size;
	if (this.stack[ptr] != null) {
		this.stptr = ptr;
		this.length--;
	}
	return this.stack[ptr];
}

function ColorStack_peek() {
	if (this.length == 0) return null;
	var ptr = (this.size + this.stptr - 1) % this.size;
	return this.stack[ptr];
}

// ---------------------------------------------------------------------------
// ColorSet

function ColorSet(id, color) {
	this.color = null;
	this.crect = new ColorRect(id);
	this.undostack = new ColorStack(25);
	this.redostack = new ColorStack(25);
	this.update = ColorSet_update;
	this.undo = ColorSet_undo;
	this.redo = ColorSet_redo;
	this.undolength = ColorSet_undolength;
	this.redolength = ColorSet_redolength;
	this.update(color, true);
	return this;
}

function ColorSet_update(c, save) {
	this.color = c;
	if (save) this.undostack.push(c);
	this.crect.update(c);
}

function ColorSet_undo() {
	var cur = this.undostack.pop();
	var prev = this.undostack.peek();
	if (prev == null) {
		this.undostack.push(cur);
		return false;
	}
    this.update(prev, false);
	this.redostack.push(cur);
	return true;
}

function ColorSet_redo() {
	var c = this.redostack.pop();
	if (c == null) return false;
	this.update(c, true);
	return true;
}

function ColorSet_undolength() {
	return this.undostack.length;
}

function ColorSet_redolength() {
	return this.redostack.length;
}

