/*
 * MyView.j
 *
 * Created by Philippe Laval on 2009/03/24.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import <AppKit/CPView.j>
@import <AppKit/CPColor.j>
@import <AppKit/CPGraphicsContext.j>


@implementation MyView : CPView
{
}

- (void)drawRect:(CPRect)aRect
{
    var bounds = [self bounds];
    var context = [[CPGraphicsContext currentContext] graphicsPort];

	/*
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColor(context, [CPColor colorWithCalibratedRed:169.0 / 255.0 green:173.0 / 255.0 blue:178.0 / 255.0 alpha:1.0]);
    CGContextStrokeRect(context, CGRectInset(CGRectIntegral(bounds), 0.5, 0.5));
    CGContextSetFillColor(context, [CPColor colorWithCalibratedRed:224.0 / 255.0 green:236.0 / 255.0 blue:250.0 / 255.0 alpha:1.0]);
    CGContextFillRect(context, CGRectInset(bounds, 2.0, 2.0));
	*/
	
	// Triangle
	
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 1.0, 4.0);
    CGContextAddLineToPoint(context, 9.0, 4.0);
    CGContextAddLineToPoint(context, 5.0, 8.0);
    CGContextAddLineToPoint(context, 1.0, 4.0);
    CGContextClosePath(context);
    
    CGContextSetFillColor(context, [CPColor colorWithCalibratedRed:0.0 / 255.0 green:236.0 / 255.0 blue:250.0 / 255.0 alpha:1.0]);
    CGContextFillPath(context);
	
	//
	
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 10.0, 10.0);
    CGContextAddLineToPoint(context, 10.0, 40.0);
    CGContextAddLineToPoint(context, 40.0, 40.0);
    CGContextAddLineToPoint(context, 20.0, 30.0);
    CGContextClosePath(context);

    CGContextSetFillColor(context, [CPColor yellowColor]);
    CGContextFillPath(context);

	CGContextSetStrokeColor(context, [CPColor greenColor]);
	CGContextStrokePath(context);

	// Ellipse
	CGContextFillEllipseInRect(context, aRect);
	CGContextStrokeEllipseInRect(context, aRect);

    CGContextSetFillColor(context, [CPColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.2]);
	// Rect
    CGContextFillRect(context, CGRectInset(aRect, 10.0, 10.0));


    CGContextBeginPath(context);
	var rect1 = CGRectMake(50, 50, 20, 20);
	var rect2 = CGRectMake(100, 100, 40, 40);
	var rects = [CPArray arrayWithObjects:rect1, rect2, nil];
	CGContextAddRects(context, rects, [rects count]);
    CGContextClosePath(context);
    CGContextSetFillColor(context, [CPColor colorWithCalibratedRed:0.0 green:0.0 blue:0.8 alpha:0.4]);
    CGContextFillPath(context);
    
}

-(void) playWithColor
{
	// CPColor is a Toll-Free bridge to CGColor
	
	/*	
	Creates a color in the RGB color space, with an alpha value.
    Each component should be between the range of 0.0 to 1.0. For
    the alpha component, a value of 1.0 is opaque, and 0.0 means
    completely transparent.
	*/
    var calibratedRgbColor = [CPColor colorWithCalibratedRed:0.0 / 255.0 green:236.0 / 255.0 blue:250.0 / 255.0 alpha:1.0];

	var calibratedWhite = [CPColor colorWithCalibratedWhite:1.0 alpha:1.0];
	
	// hue [0.0 - 360.0]
    // saturation [0.0 - 100.0]
    // brightness [0.0 - 100.0]
	var hsbColor = [CPColor colorWithHue:360.0 saturation:100.0 brightness:100.0];
	
	/*
	Creates an RGB color from a hexadecimal string. For example,
	the a string of "FFFFFF" would return a white CPColor.
	"FF0000" would return a pure red, "00FF00" would return a
	pure blue, and "0000FF" would return a pure green.
	*/	
	var rgbColor = [CPColor colorWithHexString:@"0000FF"];
	
	
	// Predefined colors
	var blackColor = [CPColor blackColor];
	var darkGrayColor = [CPColor darkGrayColor];
	var grayColor = [CPColor grayColor];
	var lightGrayColor = [CPColor lightGrayColor];
	var whiteColor = [CPColor whiteColor];
	
	var blueColor = [CPColor blueColor];
	var greenColor = [CPColor greenColor];
	var redColor = [CPColor redColor];
	var yellowColor = [CPColor yellowColor];

	var shadowColor = [CPColor shadowColor];


	// Creates a color using a tile pattern with anImage
    var anImage = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"pattern1.png"]];
	var patternColor = [CPColor colorWithPatternImage:anImage];

	// Creates a CPColor from a valid CSS RGB string. Example, "rgb(32,64,129)" or "rgb(32,64,129,255)".
	var cssColor = [CPColor colorWithCSSString:@"rgb(32,64,129)"];
	
	// Modifiy the alpha component
	var color = [rgbColor colorWithAlphaComponent:0.5]
	
	var components = [color components];
	var hsbComponents = [color hsbComponents];
	var cssString = [color cssString];
	var hexString = [color hexString];
	
}

@end

