API 0.9.5
AppKit/CPSliderColorPicker.j
Go to the documentation of this file.
00001 /*
00002  * CPApplication.j
00003  * AppKit
00004  *
00005  * Created by Ross Boucher.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 /*
00026     @ignore
00027 */
00028 @implementation CPSliderColorPicker : CPColorPicker
00029 {
00030     CPView      _contentView;
00031 
00032     CPSlider    _redSlider;
00033     CPSlider    _greenSlider;
00034     CPSlider    _blueSlider;
00035     CPSlider    _hueSlider;
00036     CPSlider    _saturationSlider;
00037     CPSlider    _brightnessSlider;
00038 
00039     CPTextField _rgbLabel;
00040     CPTextField _hsbLabel;
00041     CPTextField _redLabel;
00042     CPTextField _greenLabel;
00043     CPTextField _blueLabel;
00044     CPTextField _hueLabel;
00045     CPTextField _saturationLabel;
00046     CPTextField _brightnessLabel;
00047     CPTextField _hexLabel;
00048     CPTextField _hexValue;
00049 
00050     CPTextField _hexValue;
00051     CPTextField _redValue;
00052     CPTextField _greenValue;
00053     CPTextField _blueValue;
00054     CPTextField _hueValue;
00055     CPTextField _saturationValue;
00056     CPTextField _brightnessValue;
00057 }
00058 
00059 - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel
00060 {
00061     return [super initWithPickerMask:mask colorPanel: owningColorPanel];
00062 }
00063 
00064 - (id)initView
00065 {
00066     aFrame = CGRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
00067 
00068     _contentView = [[CPView alloc] initWithFrame:aFrame];
00069     [_contentView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00070 
00071     _rgbLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 10, 100, 20)];
00072     [_rgbLabel setStringValue: "Red, Green, Blue"];
00073     [_rgbLabel setTextColor:[CPColor blackColor]];
00074 
00075     _redLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 37, 15, 20)];
00076     [_redLabel setStringValue: "R"];
00077     [_redLabel setTextColor:[CPColor blackColor]];
00078 
00079     _redSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 35, aFrame.size.width - 70, 20)];
00080     [_redSlider setMaxValue: 1.0];
00081     [_redSlider setMinValue: 0.0];
00082     [_redSlider setTarget: self];
00083     [_redSlider setAction: @selector(sliderChanged:)];
00084     [_redSlider setAutoresizingMask: CPViewWidthSizable];
00085 
00086     // red value input box
00087     _redValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 30, 45, 28)];
00088     [_redValue setAutoresizingMask: CPViewMinXMargin];
00089     [_redValue setEditable: YES];
00090     [_redValue setBezeled: YES];
00091     [_redValue setDelegate: self];
00092     [_contentView addSubview: _redValue];
00093 
00094     _greenLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 63, 15, 20)];
00095     [_greenLabel setStringValue: "G"];
00096     [_greenLabel setTextColor:[CPColor blackColor]];
00097 
00098     _greenSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 61, aFrame.size.width - 70, 20)];
00099     [_greenSlider setMaxValue: 1.0];
00100     [_greenSlider setMinValue: 0.0];
00101     [_greenSlider setTarget: self];
00102     [_greenSlider setAction: @selector(sliderChanged:)];
00103     [_greenSlider setAutoresizingMask: CPViewWidthSizable];
00104 
00105     // green value input box
00106     _greenValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 56, 45, 28)];
00107     [_greenValue setAutoresizingMask: CPViewMinXMargin];
00108     [_greenValue setEditable: YES];
00109     [_greenValue setBezeled: YES];
00110     [_greenValue setDelegate: self];
00111     [_contentView addSubview: _greenValue];
00112 
00113     _blueLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 90, 15, 20)];
00114     [_blueLabel setStringValue: "B"];
00115     [_blueLabel setTextColor:[CPColor blackColor]];
00116 
00117     _blueSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 87, aFrame.size.width - 70, 20)];
00118     [_blueSlider setMaxValue: 1.0];
00119     [_blueSlider setMinValue: 0.0];
00120     [_blueSlider setTarget: self];
00121     [_blueSlider setAction: @selector(sliderChanged:)];
00122     [_blueSlider setAutoresizingMask: CPViewWidthSizable];
00123 
00124     // blue value input box
00125     _blueValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 82, 45, 28)];
00126     [_blueValue setAutoresizingMask: CPViewMinXMargin];
00127     [_blueValue setEditable: YES];
00128     [_blueValue setBezeled: YES];
00129     [_blueValue setDelegate: self];
00130     [_contentView addSubview: _blueValue];
00131 
00132     _hsbLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 120, 190, 20)];
00133     [_hsbLabel setStringValue: "Hue, Saturation, Brightness"];
00134     [_hsbLabel setTextColor:[CPColor blackColor]];
00135 
00136     _hueLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 145, 15, 20)];
00137     [_hueLabel setStringValue: "H"];
00138     [_hueLabel setTextColor:[CPColor blackColor]];
00139 
00140     _hueSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 143, aFrame.size.width - 70, 20)];
00141     [_hueSlider setMaxValue: 359.0];
00142     [_hueSlider setMinValue: 0.0];
00143     [_hueSlider setTarget: self];
00144     [_hueSlider setAction: @selector(sliderChanged:)];
00145     [_hueSlider setAutoresizingMask: CPViewWidthSizable];
00146 
00147     // hue value input box
00148     _hueValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 138, 45, 28)];
00149     [_hueValue setAutoresizingMask: CPViewMinXMargin];
00150     [_hueValue setEditable: YES];
00151     [_hueValue setBezeled: YES];
00152     [_hueValue setDelegate: self];
00153     [_contentView addSubview: _hueValue];
00154 
00155     _saturationLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 170, 15, 20)];
00156     [_saturationLabel setStringValue: "S"];
00157     [_saturationLabel setTextColor:[CPColor blackColor]];
00158 
00159     _saturationSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 168, aFrame.size.width - 70, 20)];
00160     [_saturationSlider setMaxValue: 100.0];
00161     [_saturationSlider setMinValue: 0.0];
00162     [_saturationSlider setTarget: self];
00163     [_saturationSlider setAction: @selector(sliderChanged:)];
00164     [_saturationSlider setAutoresizingMask: CPViewWidthSizable];
00165 
00166     // saturation value input box
00167     _saturationValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 164, 45, 28)];
00168     [_saturationValue setAutoresizingMask: CPViewMinXMargin];
00169     [_saturationValue setEditable: YES];
00170     [_saturationValue setBezeled: YES];
00171     [_saturationValue setDelegate: self];
00172     [_contentView addSubview: _saturationValue];
00173 
00174     _brightnessLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 196, 15, 20)];
00175     [_brightnessLabel setStringValue: "B"];
00176     [_brightnessLabel setTextColor:[CPColor blackColor]];
00177 
00178     _brightnessSlider = [[CPSlider alloc] initWithFrame: CGRectMake(15, 194, aFrame.size.width - 70, 20)];
00179     [_brightnessSlider setMaxValue: 100.0];
00180     [_brightnessSlider setMinValue: 0.0];
00181     [_brightnessSlider setTarget: self];
00182     [_brightnessSlider setAction: @selector(sliderChanged:)];
00183     [_brightnessSlider setAutoresizingMask: CPViewWidthSizable];
00184 
00185     // brightness value input box
00186     _brightnessValue = [[CPTextField alloc] initWithFrame: CGRectMake(aFrame.size.width - 45, 190, 45, 28)];
00187     [_brightnessValue setAutoresizingMask: CPViewMinXMargin];
00188     [_brightnessValue setEditable: YES];
00189     [_brightnessValue setBezeled: YES];
00190     [_brightnessValue setDelegate: self];
00191     [_contentView addSubview: _brightnessValue];
00192 
00193     _hexLabel = [[CPTextField alloc] initWithFrame: CGRectMake(0, 230, 30, 20)];
00194     [_hexLabel setStringValue: "Hex"];
00195     [_hexLabel setTextColor:[CPColor blackColor]];
00196 
00197     //hex input box
00198     _hexValue = [[CPTextField alloc] initWithFrame: CGRectMake(32, 225, 80, 28)];
00199     [_hexValue setEditable: YES];
00200     [_hexValue setBezeled: YES];
00201     [_hexValue setDelegate: self];
00202     [_contentView addSubview: _hexValue];
00203 
00204     [_contentView addSubview: _rgbLabel];
00205     [_contentView addSubview: _redLabel];
00206     [_contentView addSubview: _greenLabel];
00207     [_contentView addSubview: _blueLabel];
00208     [_contentView addSubview: _redSlider];
00209     [_contentView addSubview: _greenSlider];
00210     [_contentView addSubview: _blueSlider];
00211 
00212     [_contentView addSubview: _hsbLabel];
00213     [_contentView addSubview: _hueLabel];
00214     [_contentView addSubview: _saturationLabel];
00215     [_contentView addSubview: _brightnessLabel];
00216     [_contentView addSubview: _hueSlider];
00217     [_contentView addSubview: _saturationSlider];
00218     [_contentView addSubview: _brightnessSlider];
00219 
00220     [_contentView addSubview: _hexLabel];
00221 }
00222 
00223 - (CPView)provideNewView:(BOOL)initialRequest
00224 {
00225     if (initialRequest)
00226         [self initView];
00227 
00228     return _contentView;
00229 }
00230 
00231 - (int)currentMode
00232 {
00233     return CPSliderColorPickerMode;
00234 }
00235 
00236 - (BOOL)supportsMode:(int)mode
00237 {
00238     return (mode == CPSliderColorPickerMode) ? YES : NO;
00239 }
00240 
00241 - (void)sliderChanged:(id)sender
00242 {
00243     var newColor,
00244         colorPanel = [self colorPanel],
00245         alpha = [colorPanel opacity];
00246 
00247     switch (sender)
00248     {
00249         case    _hueSlider:
00250         case    _saturationSlider:
00251         case    _brightnessSlider:      newColor = [CPColor colorWithHue: [_hueSlider floatValue]
00252                                                               saturation: [_saturationSlider floatValue]
00253                                                               brightness: [_brightnessSlider floatValue]
00254                                                                    alpha: alpha];
00255 
00256                                         [self updateRGBSliders: newColor];
00257                                         break;
00258 
00259         case    _redSlider:
00260         case    _greenSlider:
00261         case    _blueSlider:            newColor = [CPColor colorWithCalibratedRed: [_redSlider floatValue]
00262                                                                              green: [_greenSlider floatValue]
00263                                                                               blue: [_blueSlider floatValue]
00264                                                                              alpha: alpha];
00265 
00266                                         [self updateHSBSliders: newColor];
00267                                         break;
00268     }
00269 
00270     [self updateLabels];
00271     [self updateHex: newColor];
00272     [colorPanel setColor: newColor];
00273 }
00274 
00275 - (void)setColor:(CPColor)aColor
00276 {
00277     [self updateRGBSliders: aColor];
00278     [self updateHSBSliders: aColor];
00279     [self updateHex: aColor];
00280     [self updateLabels];
00281 }
00282 
00283 - (void)updateHSBSliders:(CPColor)aColor
00284 {
00285     var hsb = [aColor hsbComponents];
00286 
00287     [_hueSlider setFloatValue:hsb[0]];
00288     [_saturationSlider setFloatValue:hsb[1]];
00289     [_brightnessSlider setFloatValue:hsb[2]];
00290 }
00291 
00292 - (void)updateHex:(CPColor)aColor
00293 {
00294     [_hexValue setStringValue:[aColor hexString]];
00295 }
00296 
00297 - (void)updateRGBSliders:(CPColor)aColor
00298 {
00299     var rgb = [aColor components];
00300 
00301     [_redSlider setFloatValue:rgb[0]];
00302     [_greenSlider setFloatValue:rgb[1]];
00303     [_blueSlider setFloatValue:rgb[2]];
00304 }
00305 
00306 - (void)updateLabels
00307 {
00308     [_hueValue setStringValue: ROUND([_hueSlider floatValue])];
00309     [_saturationValue setStringValue: ROUND([_saturationSlider floatValue])];
00310     [_brightnessValue setStringValue: ROUND([_brightnessSlider floatValue])];
00311 
00312     [_redValue setStringValue: ROUND([_redSlider floatValue] * 255)];
00313     [_greenValue setStringValue: ROUND([_greenSlider floatValue] * 255)];
00314     [_blueValue setStringValue: ROUND([_blueSlider floatValue] * 255)];
00315 }
00316 
00317 - (CPImage)provideNewButtonImage
00318 {
00319     return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"slider_button.png"] size:CGSizeMake(32, 32)];
00320 }
00321 
00322 - (CPImage)provideNewAlternateButtonImage
00323 {
00324     return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"slider_button_h.png"] size:CGSizeMake(32, 32)];
00325 }
00326 
00327 - (void)controlTextDidEndEditing:(CPNotification)aNotification
00328 {
00329     var field = [aNotification object],
00330         value = [[field stringValue] stringByTrimmingWhitespace];
00331 
00332     if (field === _hexValue)
00333     {
00334         var newColor = [CPColor colorWithHexString: value];
00335 
00336         if (newColor)
00337         {
00338             [self setColor: newColor];
00339             [[self colorPanel] setColor: newColor];
00340         }
00341     }
00342     else
00343     {
00344         switch (field)
00345         {
00346             case _redValue:        [_redSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
00347                                    [self sliderChanged: _redSlider];
00348                                    break;
00349 
00350             case _greenValue:      [_greenSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
00351                                    [self sliderChanged: _greenSlider];
00352                                    break;
00353 
00354             case _blueValue:       [_blueSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
00355                                    [self sliderChanged: _blueSlider];
00356                                    break;
00357 
00358             case _hueValue:        [_hueSlider setFloatValue:MAX(MIN(ROUND(value), 360), 0)];
00359                                    [self sliderChanged: _hueSlider];
00360                                    break;
00361 
00362             case _saturationValue: [_saturationSlider setFloatValue:MAX(MIN(ROUND(value), 100), 0)];
00363                                    [self sliderChanged: _saturationSlider];
00364                                    break;
00365 
00366             case _brightnessValue: [_brightnessSlider setFloatValue:MAX(MIN(ROUND(value), 100), 0)];
00367                                    [self sliderChanged: _brightnessSlider];
00368                                    break;
00369         }
00370     }
00371 }
00372 
00373 @end
 All Classes Files Functions Variables Defines