1//
2//  PRHisto.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on Thu Dec 18 2003.
6//  Copyright (c) 2003-2014 Carduus. All rights reserved.
7//
8// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10
11#import "PRHisto.h"
12
13
14@implementation PRHisto
15
16- (void)displayHistogram :(NSRect)viewRect
17{
18    NSRect bar;
19    NSBezierPath *bez;
20    NSBezierPath *redBez, *greenBez, *blueBez;
21    NSColor *fillRed, *fillGreen, *fillBlue;
22    int i;
23    float barWidth;
24    float barHeightScale;
25
26    NSLog(@"Standard Histogram drawing");
27    NSLog(@"maxHisto: %f", maxHisto);
28
29    barWidth = viewRect.size.width / UCHAR_MAX;
30    barHeightScale =  viewRect.size.height / maxHisto;
31    NSLog(@"frame height: %f", viewRect.size.height);
32    NSLog(@"scale: %f", barHeightScale);
33    bez = [NSBezierPath bezierPath];
34    redBez = [NSBezierPath bezierPath];
35    greenBez = [NSBezierPath bezierPath];
36    blueBez = [NSBezierPath bezierPath];
37
38    if (hasColor)
39    {
40        fillRed = [NSColor redColor];
41        fillGreen = [NSColor greenColor];
42        fillBlue = [NSColor blueColor];
43        if (1)
44        {
45            for(i = 0; i < UCHAR_MAX; i++)
46            {
47                float redHeight;
48                float greenHeight;
49                float blueHeight;
50                redHeight   = histogramR[i]*barHeightScale;
51                greenHeight = histogramG[i]*barHeightScale;
52                blueHeight  = histogramB[i]*barHeightScale;
53                bar = NSMakeRect(i*barWidth, 0, barWidth, greenHeight);
54                [greenBez appendBezierPathWithRect:bar];
55                bar = NSMakeRect(i*barWidth, greenHeight, barWidth, redHeight);
56                [redBez appendBezierPathWithRect:bar];
57                bar = NSMakeRect(i*barWidth, greenHeight + redHeight, barWidth, blueHeight);
58                [blueBez appendBezierPathWithRect:bar];
59            }
60            [fillRed set];
61            [redBez fill];
62            [fillGreen set];
63            [greenBez fill];
64            [fillBlue set];
65            [blueBez fill];
66        } else
67        {
68            for(i = 0; i < UCHAR_MAX; i++)
69            {
70                if (histogramR[i] > histogramG[i])
71                {
72                    if (histogramG[i] > histogramB[i])
73                    {
74                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramG[i]*barHeightScale);
75                        [greenBez appendBezierPathWithRect:bar];
76                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramB[i]*barHeightScale);
77                        [blueBez appendBezierPathWithRect:bar];
78                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramR[i]*barHeightScale);
79                        [redBez appendBezierPathWithRect:bar];
80                    } else
81                    {
82                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramB[i]*barHeightScale);
83                        [blueBez appendBezierPathWithRect:bar];
84                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramG[i]*barHeightScale);
85                        [greenBez appendBezierPathWithRect:bar];
86                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramR[i]*barHeightScale);
87                        [redBez appendBezierPathWithRect:bar];
88                    }
89                }
90                else
91                {
92                    if (histogramG[i] > histogramB[i])
93                    {
94                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramG[i]*barHeightScale);
95                        [greenBez appendBezierPathWithRect:bar];
96                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramB[i]*barHeightScale);
97                        [blueBez appendBezierPathWithRect:bar];
98                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramR[i]*barHeightScale);
99                        [redBez appendBezierPathWithRect:bar];
100                    } else
101                    {
102                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramB[i]*barHeightScale);
103                        [blueBez appendBezierPathWithRect:bar];
104                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramG[i]*barHeightScale);
105                        [greenBez appendBezierPathWithRect:bar];
106                        bar = NSMakeRect(i*barWidth, 0, barWidth, histogramR[i]*barHeightScale);
107                        [redBez appendBezierPathWithRect:bar];
108                    }
109                }
110            }
111            [fillRed set];
112            [redBez fill];
113            [fillGreen set];
114            [greenBez fill];
115            [fillBlue set];
116            [blueBez fill];
117        }
118    } else /* greyscale */
119    {
120        for(i = 0; i < UCHAR_MAX; i++)
121        {
122            bar = NSMakeRect(i*barWidth, 0, barWidth, histogram[i]*barHeightScale);
123            [bez appendBezierPathWithRect:bar];
124        }
125        [bez fill];
126    }
127}
128
129@end
130