#import  "TonePanelCtrl.h"
#import  <appkit/Control.h>
#import  <appkit/Matrix.h>
#import  <stdio.h>
#import  "../common.h"
#import  "../ImageOpCtr.h"
#import  "../ImageOpr.h"


@implementation TonePanelCtrl

- init
{
	return [self reset:nil];
}

- reset: sender
{
	int	i;

	if (sender && [sender selectedTag] != 0)
		[mainSlider setFloatValue: 1.0];
	for (i = 0; i < N_Colors; i++)
		ratios[i] = 1.0;
	return [self changeMainValue: self];
}

/* Local Method */
- setFormatFloat:(float)value to:target
{
	char	buf[16];

	sprintf(buf, "%4.2f", (int)(value * 20.0) / 20.0);
	return [target setStringValue: buf];
}

- changeMainValue: sender
{
	int	i;
	float	v, cv;

	v = [mainSlider floatValue];
	[self setFormatFloat:v to:mainIndicator];
	for (i = 0; i < N_Colors; i++) {
		cv = v * ratios[i];
		if (cv > Colors_MAXVAL) cv = Colors_MAXVAL;
		[[colorSliders cellAt:i :0] setFloatValue: cv];
		[self setFormatFloat:cv to:[colorIndicators cellAt:i :0]];
	}
	return self;
}

- changeColorValue: sender
{
	id	cell;
	int	tag;
	float	v, mv;

	cell = [sender selectedCell];
	tag = [cell tag];
	v = [cell floatValue];
	mv = [mainSlider floatValue];
	ratios[tag] = (mv > 0.01) ? (v / mv) : 1.0;
	[self setFormatFloat:v to:[colorIndicators cellAt:tag :0]];
	return self;
}

- colorTone: sender
{
	int	i;
	float	values[N_Colors];

	for (i = 0; i < N_Colors; i++)
		values[i] = [[colorIndicators cellAt:i :0] floatValue];
	[imageOpCtr colorToneEnhance: values];
	/* This message will be delegated to ImageOpr */
	return self;
}

@end
