//
//  IFXPDFColorsSample.m
//  IFXPDFFactory-iOS-SamplesBrowser
//
//  Created by Sorin Nistor on 11/6/13.
//  Copyright (c) 2013 IFXFactory. All rights reserved.
//

#import "IFXPDFColorsSample.h"

@implementation IFXPDFColorsSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];

    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    
    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaBoldFontFace;
    titleFont.size = 16;
    IFXPDFFont* sectionFont = [[IFXPDFFont alloc] init];
    sectionFont.fontFace = IFXPDFHelveticaFontFace;
    sectionFont.size = 10;
    
    [page.graphics drawText:@"Colors & Colorspaces" withFont:titleFont brush:blackBrush atX:20 y:50];

    [page.graphics drawText:@"DeviceRGB" withFont:sectionFont brush:blackBrush atX:20 y:70];
    IFXPDFPen* rgbPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor darkRedColor] andWidth:4];
    IFXPDFBrush* rgbBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor lightPinkColor]];
    [page.graphics drawRectangleWithPen:rgbPen andBrush:rgbBrush atX:20 y:85 withWidth:250 height:100];
    
    [page.graphics drawText:@"DeviceCMYK" withFont:sectionFont brush:blackBrush atX:340 y:70];
    IFXPDFPen* cmykPen = [IFXPDFPen penWithColor:[IFXPDFCmykColor cmykColorWithC:1 m:0.5 y:0.3 k:0.7] andWidth:4];
    IFXPDFBrush* cmykBrush = [IFXPDFBrush brushWithColor:[IFXPDFCmykColor cmykColorWithC:0 m:0.5 y:0.34 k:0.1]];
    [page.graphics drawRectangleWithPen:cmykPen andBrush:cmykBrush atX:340 y:85 withWidth:250 height:100];

    [page.graphics drawText:@"DeviceGray" withFont:sectionFont brush:blackBrush atX:20 y:200];
    IFXPDFPen* grayPen = [IFXPDFPen penWithColor:[IFXPDFGrayColor grayColorWithGray:0.1] andWidth:4];
    IFXPDFBrush* grayBrush = [IFXPDFBrush brushWithColor:[IFXPDFGrayColor grayColorWithGray:0.8]];
    [page.graphics drawRectangleWithPen:grayPen andBrush:grayBrush atX:20 y:215 withWidth:250 height:100];
    
    [page.graphics drawText:@"Indexed" withFont:sectionFont brush:blackBrush atX:340 y:200];
    IFXPDFIndexedColorSpace* indexedColorSpace = [[IFXPDFIndexedColorSpace alloc] init];
    indexedColorSpace.maxIndex = 1;
    indexedColorSpace.baseColorSpace = [IFXPDFRgbColorSpace rgbColorSpace];
    char colorTable[6] = { 192, 0, 0, 0, 0, 128 };
    indexedColorSpace.colorTable = [NSData dataWithBytes:colorTable length:6];
    IFXPDFIndexedColor* indexedColor0 = [IFXPDFIndexedColor indexedColorWithColorSpace:indexedColorSpace andColorIndex:0];
    IFXPDFIndexedColor* indexedColor1 = [IFXPDFIndexedColor indexedColorWithColorSpace:indexedColorSpace andColorIndex:1];
    IFXPDFPen* indexedPen = [IFXPDFPen penWithColor:indexedColor0 andWidth:4];
    IFXPDFBrush* indexedBrush = [IFXPDFBrush brushWithColor:indexedColor1];
    [page.graphics drawRectangleWithPen:indexedPen andBrush:indexedBrush atX:340 y:215 withWidth:250 height:100];

    [page.graphics drawText:@"CalGray" withFont:sectionFont brush:blackBrush atX:20 y:330];
    IFXPDFCalGrayColorSpace* calGrayColorSpace = [[IFXPDFCalGrayColorSpace alloc] init];
    IFXPDFCalGrayColor* calGrayColor1 = [[IFXPDFCalGrayColor alloc] initWithCalGrayColorSpace:calGrayColorSpace];
    calGrayColor1.gray = 0.6;
    IFXPDFCalGrayColor* calGrayColor2 = [[IFXPDFCalGrayColor alloc] initWithCalGrayColorSpace:calGrayColorSpace];
    calGrayColor2.gray = 0.2;
    IFXPDFPen* calGrayPen = [IFXPDFPen penWithColor:calGrayColor1 andWidth:4];
    IFXPDFBrush* calGrayBrush = [IFXPDFBrush brushWithColor:calGrayColor2];
    [page.graphics drawRectangleWithPen:calGrayPen andBrush:calGrayBrush atX:20 y:345 withWidth:250 height:100];
    
    [page.graphics drawText:@"CalRGB" withFont:sectionFont brush:blackBrush atX:340 y:330];
    IFXPDFCalRGBColorSpace* calRgbColorSpace = [[IFXPDFCalRGBColorSpace alloc] init];
    IFXPDFCalRGBColor* calRgbColor1 = [IFXPDFCalRGBColor calRGBColorWithColorSpace:calRgbColorSpace
                                                                            andRed:0.1 green:0.5 blue:0.25];
    IFXPDFCalRGBColor* calRgbColor2 = [IFXPDFCalRGBColor calRGBColorWithColorSpace:calRgbColorSpace
                                                                            andRed:0.6 green:0.1 blue:0.9];
    IFXPDFPen* calRgbPen = [IFXPDFPen penWithColor:calRgbColor1 andWidth:4];
    IFXPDFBrush* calRgbBrush = [IFXPDFBrush brushWithColor:calRgbColor2];
    [page.graphics drawRectangleWithPen:calRgbPen andBrush:calRgbBrush atX:340 y:345 withWidth:250 height:100];

    [page.graphics drawText:@"L*a*b" withFont:sectionFont brush:blackBrush atX:20 y:460];
    IFXPDFLabColorSpace* labColorSpace = [[IFXPDFLabColorSpace alloc] init];
    IFXPDFLabColor* labColor1 = [IFXPDFLabColor labColorWithColorSpace:labColorSpace andL:90 a:-40 b:120];
    IFXPDFLabColor* labColor2 = [IFXPDFLabColor labColorWithColorSpace:labColorSpace andL:45 a:90 b:-34];
    IFXPDFPen* labPen = [IFXPDFPen penWithColor:labColor1 andWidth:4];
    IFXPDFBrush* labBrush = [IFXPDFBrush brushWithColor:labColor2];
    [page.graphics drawRectangleWithPen:labPen andBrush:labBrush atX:20 y:475 withWidth:250 height:100];
    
    [page.graphics drawText:@"ICC" withFont:sectionFont brush:blackBrush atX:340 y:460];
    IFXPDFIccColorSpace* iccColorSpace = [[IFXPDFIccColorSpace alloc] init];
    NSString *iccPath = [[NSBundle mainBundle] pathForResource:@"rgb" ofType:@"icc"];
    NSData* iccData = [NSData dataWithContentsOfFile:iccPath];
    iccColorSpace.profileData = iccData;
    iccColorSpace.alternateColorSpace = [IFXPDFRgbColorSpace rgbColorSpace];
    iccColorSpace.numberOfColorComponents = 3;
    IFXPDFIccColor* iccColor1 = [IFXPDFIccColor iccColorWithColorSpace:iccColorSpace];
    iccColor1.colors[0] = 0.45;
    iccColor1.colors[1] = 0.1;
    iccColor1.colors[2] = 0.22;
    IFXPDFIccColor* iccColor2 = [IFXPDFIccColor iccColorWithColorSpace:iccColorSpace];
    iccColor2.colors[0] = 0.21;
    iccColor2.colors[1] = 0.76;
    iccColor2.colors[2] = 0.31;
    IFXPDFPen* iccPen = [IFXPDFPen penWithColor:iccColor1 andWidth:4];
    IFXPDFBrush* iccBrush = [IFXPDFBrush brushWithColor:iccColor2];
    [page.graphics drawRectangleWithPen:iccPen andBrush:iccBrush atX:340 y:475 withWidth:250 height:100];

    [page.graphics drawText:@"Separation" withFont:sectionFont brush:blackBrush atX:20 y:590];
    IFXPDFExponentialFunction* tintTransform = [[IFXPDFExponentialFunction alloc] init];
    double domain[2] = { 0, 1 };
    tintTransform.domain = [IFXPDFNumberArray arrayWithNumbers:domain length:2];
    double range[8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
    tintTransform.range = [IFXPDFNumberArray arrayWithNumbers:range length:8];
    tintTransform.exponent = 1;
    double c0[4] = { 0, 0, 0, 0 };
    tintTransform.c0 = [IFXPDFNumberArray arrayWithNumbers:c0 length:4];
    double c1[4] = { 1, 0.5, 0, 0.1 };
    tintTransform.c1 = [IFXPDFNumberArray arrayWithNumbers:c1 length:4];
    
    IFXPDFSeparationColorSpace* separationColorSpace = [[IFXPDFSeparationColorSpace alloc] init];
    separationColorSpace.alternateColorSpace = [IFXPDFCmykColorSpace cmykColorSpace];
    separationColorSpace.colorant = @"CustomBlue";
    separationColorSpace.tintTransform = tintTransform;
    
    IFXPDFSeparationColor* separationColor1 = [IFXPDFSeparationColor separationColorWithColorSpace:separationColorSpace andTint:0.23];
    IFXPDFSeparationColor* separationColor2 = [IFXPDFSeparationColor separationColorWithColorSpace:separationColorSpace andTint:0.74];
    
    IFXPDFPen* separationPen = [IFXPDFPen penWithColor:separationColor1 andWidth:4];
    IFXPDFBrush* separationBrush = [IFXPDFBrush brushWithColor:separationColor2];
    [page.graphics drawRectangleWithPen:separationPen andBrush:separationBrush atX:20 y:605 withWidth:250 height:100];

    return pdfDocument;
}

@end