//
//	MiscHalftoneColor.m -- a class for color "tints"  (to specify a halftone)
//		Written by Don Yacktman (c) 1994 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/misckit.h>

@implementation MiscHalftoneColor

+ (BOOL)canInitFromPasteboard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super canInitFromPasteboard:pasteboard];
}

- setFromPasteBoard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super setFromPasteBoard:pasteboard];
}

- writeToPasteBoard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super writeToPasteBoard:pasteboard];
}

- setHalftone:(float)amount
{
	percent = MAX(MIN(amount, 1.0), 0.0);
	return self;
}

- (float)halftone { return percent; }

- (NXColor)filterColor:(NXColor)input
{ // halftone the color to print before printing it:
	float c, m, y, k;
	id workingColor = [MiscColor newColor:input];
	[workingColor getCyan:&c magenta:&m yellow:&y black:&k];
	[workingColor setCyan:(c * percent) magenta:(m * percent)
			yellow:(y * percent) black:(k * percent)];
	return [workingColor color];
}

- setColorFromObject:object
{
	if ([super setColorFromObject:object]) {
		// preserve halftoning info if available, if not, assume 100% color
		if ([object respondsTo:@selector(halftone)])
			percent = [object halftone];
		else percent = 1.0;
		return self;
	}
	return nil;
}

- (BOOL)isEqualHalftoneAmount:anObject
{
	if ([anObject respondsTo:@selector(halftone)])
		if ([anObject halftone] == percent) return YES;
	return NO;
}

- (BOOL)isEqualColor:anObject
{
	if ([super isEqualColor:anObject]) {
		return [self isEqualHalftoneAmount:anObject];
	} else {
		if ([anObject respondsTo:@selector(isEqualToColor:)])
			return [anObject isEqualToColor:[self filterColor:theColor]];
		else if ([anObject respondsTo:@selector(color)]) {
			id tempColor = [MiscColor newColor:[self filterColor:theColor]];
			BOOL ret = [tempColor isEqualToColor:[anObject color]];
			[tempColor free];
			return ret;
		}
	}
	return NO;
}

- (BOOL)isEqual:anObject
{
	if ([super isEqual:anObject]) {
		return [self isEqualHalftoneAmount:anObject];
	}
	return NO;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	NXReadTypes(stream, "f", &percent);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteTypes(stream, "f", &percent);
	return self;
}

- encodeUsing:(id <NXEncoding>)portal
{
	[super encodeUsing:portal];
	[portal encodeData:&percent ofType:"f"];
	return self;
}

- decodeUsing:(id <NXDecoding>)portal
{
	[super decodeUsing:portal];
	[portal decodeData:&percent ofType:"f"];
	return self;
}

- encodeRemotelyFor:(NXConnection *)connection
	freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
{
	return [super encodeRemotelyFor:connection
				freeAfterEncoding:flagp isBycopy:isByCopy];
}

- (const char *)getInspectorClassName
{
	return "MiscHalftoneColorInspector";
}

- (NXImage *)getIBImage
{
	return [NXImage findImageNamed:"MiscHalftoneColorObj"];
}

- setColorFromPixel:(NXPoint *)location
{
	[super setColorFromPixel:location];
	percent = 1.0;
	return self;
}

@end