home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscColor.m < prev    next >
Encoding:
Text File  |  1994-01-25  |  10.4 KB  |  440 lines

  1. //
  2. //    MiscColor.m -- a wrapper class around the NXColor functions
  3. //        Written by Don Yacktman (c) 1994 by Don Yacktman.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. #import <misckit/misckit.h>
  15.  
  16. @implementation MiscColor
  17.  
  18. + (BOOL)canInitFromPasteboard:(Pasteboard *)pasteboard
  19. { // ***** need to also check for the color object type
  20.     const char *pBoardTypes[2] = { NXColorPboardType, NULL };
  21.     if ([pasteboard findAvailableTypeFrom:pBoardTypes num:1]
  22.             == NXColorPboardType) return YES;
  23.     return NO;
  24. }
  25.  
  26. + newFromPasteBoard:(Pasteboard *)pasteboard
  27. {
  28.     return [[self alloc] initFromPasteBoard:pasteboard];
  29. }
  30.  
  31. + newColor:(NXColor)color
  32. {
  33.     return [[self alloc] initColor:color];
  34. }
  35.  
  36. - init
  37. {
  38.     return [self initColor:NX_COLORWHITE];
  39. }
  40.  
  41. - initColor:(NXColor)color
  42. {
  43.     [self setColor:color];
  44.     [self setDelegate:nil];
  45.     return self;
  46. }
  47.  
  48. - initFromPasteBoard:(Pasteboard *)pasteboard
  49. {
  50.     if (![[self class] canInitFromPasteboard:pasteboard]) {
  51.         [self free];
  52.         return nil;
  53.     }
  54.     return [[self init] setFromPasteBoard:pasteboard];
  55. }
  56.  
  57. - initColorFromPixel:(NXPoint *)location
  58. {
  59.     return [self initColor:NXReadPixel(location)];
  60. }
  61.  
  62. - readFromPasteBoard:(Pasteboard *)pasteboard
  63. { // ***** need to also deal with color object type
  64.     [self setColor:NXReadColorFromPasteboard(pasteboard)];
  65.     return self;
  66. }
  67.  
  68. - setFromPasteBoard:(Pasteboard *)pasteboard
  69. {
  70.     return [self readFromPasteBoard:pasteboard];
  71. }
  72.  
  73. - writeToPasteBoard:(Pasteboard *)pasteboard
  74. {    // Need to add a pasteboard type for the color OBJECT, too... *****
  75.     NXWriteColorToPasteboard(pasteboard, [self actualColor]);
  76.     return self;
  77. }
  78.  
  79. - setRedComponent:(float)red
  80. {
  81.     [self setColor:NXChangeRedComponent([self actualColor], red)];
  82.     return self;
  83. }
  84.  
  85. - setGreenComponent:(float)green
  86. {
  87.     [self setColor:NXChangeGreenComponent([self actualColor], green)];
  88.     return self;
  89. }
  90.  
  91. - setBlueComponent:(float)blue
  92. {
  93.     [self setColor:NXChangeBlueComponent([self actualColor], blue)];
  94.     return self;
  95. }
  96.  
  97. - setCyanComponent:(float)cyan
  98. {
  99.     [self setColor:NXChangeCyanComponent([self actualColor], cyan)];
  100.     return self;
  101. }
  102.  
  103. - setMagentaComponent:(float)magenta
  104. {
  105.     [self setColor:NXChangeMagentaComponent([self actualColor], magenta)];
  106.     return self;
  107. }
  108.  
  109. - setYellowComponent:(float)yellow
  110. {
  111.     [self setColor:NXChangeYellowComponent([self actualColor], yellow)];
  112.     return self;
  113. }
  114.  
  115. - setBlackComponent:(float)black
  116. {
  117.     [self setColor:NXChangeBlackComponent([self actualColor], black)];
  118.     return self;
  119. }
  120.  
  121. - setHueComponent:(float)hue
  122. {
  123.     [self setColor:NXChangeHueComponent([self actualColor], hue)];
  124.     return self;
  125. }
  126.  
  127. - setSaturationComponent:(float)saturation
  128. {
  129.     [self setColor:
  130.             NXChangeSaturationComponent([self actualColor], saturation)];
  131.     return self;
  132. }
  133.  
  134. - setBrightnessComponent:(float)brightness
  135. {
  136.     [self setColor:
  137.             NXChangeBrightnessComponent([self actualColor], brightness)];
  138.     return self;
  139. }
  140.  
  141. - setGrayComponent:(float)gray
  142. {
  143.     [self setColor:NXChangeGrayComponent([self actualColor], gray)];
  144.     return self;
  145. }
  146.  
  147. - setAlphaComponent:(float)alpha
  148. {
  149.     [self setColor:NXChangeAlphaComponent([self actualColor], alpha)];
  150.     return self;
  151. }
  152.  
  153. - setColor:(NXColor)color
  154. {
  155.     theColor = color;
  156.     return self;
  157. }
  158.  
  159. - setColorFromObject:object
  160. {
  161.     if ([object respondsTo:@selector(actualColor)]) {
  162.         [self setColor:[object actualColor]];
  163.         return self;
  164.     } else if ([object respondsTo:@selector(color)]) {
  165.         [self setColor:[object color]];
  166.         return self;
  167.     }
  168.     return nil;
  169. }
  170.  
  171. - setColorFromPixel:(NXPoint *)location
  172. {
  173.     [self setColor:NXReadPixel(location)];
  174.     return self;
  175. }
  176.  
  177. - (NXColor)color { return [self filterColor:[self actualColor]]; }
  178. - (NXColor)actualColor { return theColor; }
  179.  
  180. - display
  181. {
  182.     [self setColor]; // throw away return value
  183.     return self;
  184. }
  185.  
  186. - (NXColor)filterColor:(NXColor)input
  187. {
  188.     return input;
  189. }
  190.  
  191. - (BOOL)setColor
  192. {
  193.     BOOL printOK = YES;
  194.     id theDelegate = [self delegate];
  195.     NXColor realColor = [self actualColor];
  196.     if ([theDelegate respondsTo:@selector(willSendColorToPS:shouldPrint:)])
  197.         realColor = [theDelegate willSendColorToPS:[self actualColor]
  198.                 shouldPrint:&printOK];
  199.     if (printOK) {
  200.         NXColor filtered = [self filterColor:realColor];
  201.         NXSetColor(filtered);
  202.         if ([theDelegate respondsTo:@selector(didSendColorToPS:)])
  203.             [theDelegate didSendColorToPS:filtered];
  204.     }
  205.     return printOK;
  206. }
  207.  
  208. - getGray:(float *)gray
  209. {
  210.     NXConvertColorToGray([self actualColor], gray);
  211.     return self;
  212. }
  213.  
  214. - getGray:(float *)gray alpha:(float *)alpha
  215. {
  216.     NXConvertColorToGrayAlpha([self actualColor], gray, alpha);
  217.     return self;
  218. }
  219.  
  220. - getRed:(float *)red green:(float *)green blue:(float *)blue
  221. {
  222.     NXConvertColorToRGB([self actualColor], red, green, blue);
  223.     return self;
  224. }
  225.  
  226. - getRed:(float *)red green:(float *)green blue:(float *)blue
  227.         alpha:(float *)alpha
  228. {
  229.     NXConvertColorToRGBA([self actualColor], red, green, blue, alpha);
  230.     return self;
  231. }
  232.  
  233. - getHue:(float *)hue saturation:(float *)saturation
  234.         brightness:(float *)brightness
  235. {
  236.     NXConvertColorToHSB([self actualColor], hue, saturation, brightness);
  237.     return self;
  238. }
  239.  
  240. - getHue:(float *)hue saturation:(float *)saturation
  241.         brightness:(float *)brightness alpha:(float *)alpha
  242. {
  243.     NXConvertColorToHSBA([self actualColor],
  244.             hue, saturation, brightness, alpha);
  245.     return self;
  246. }
  247.  
  248. - getCyan:(float *)cyan magenta:(float *)magenta yellow:(float *)yellow
  249.         black:(float *)black
  250. {
  251.     NXConvertColorToCMYK([self actualColor], cyan, magenta, yellow, black);
  252.     return self;
  253. }
  254.  
  255. - getCyan:(float *)cyan magenta:(float *)magenta yellow:(float *)yellow
  256.         black:(float *)black alpha:(float *)alpha
  257. {
  258.     NXConvertColorToCMYKA([self actualColor],
  259.             cyan, magenta, yellow, black, alpha);
  260.     return self;
  261. }
  262.  
  263. - setGray:(float)gray
  264. {
  265.     [self setColor:NXConvertGrayToColor(gray)];
  266.     return self;
  267. }
  268.  
  269. - setGray:(float)gray alpha:(float)alpha
  270. {
  271.     [self setColor:NXConvertGrayAlphaToColor(gray, alpha)];
  272.     return self;
  273. }
  274.  
  275. - setRed:(float)red green:(float)green blue:(float)blue
  276. {
  277.     [self setColor:NXConvertRGBToColor(red, green, blue)];
  278.     return self;
  279. }
  280.  
  281. - setRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha
  282. {
  283.     [self setColor:NXConvertRGBAToColor(red, green, blue, alpha)];
  284.     return self;
  285. }
  286.  
  287. - setHue:(float)hue saturation:(float)saturation
  288.         brightness:(float)brightness
  289. {
  290.     [self setColor:NXConvertHSBToColor(hue, saturation, brightness)];
  291.     return self;
  292. }
  293.  
  294. - setHue:(float)hue saturation:(float)saturation
  295.         brightness:(float)brightness alpha:(float)alpha
  296. {
  297.     [self setColor:NXConvertHSBAToColor(hue, saturation, brightness, alpha)];
  298.     return self;
  299. }
  300.  
  301. - setCyan:(float)cyan magenta:(float)magenta yellow:(float)yellow
  302.         black:(float)black
  303. {
  304.     [self setColor:NXConvertCMYKToColor(cyan, magenta, yellow, black)];
  305.     return self;
  306. }
  307.  
  308. - setCyan:(float)cyan magenta:(float)magenta yellow:(float)yellow
  309.         black:(float)black alpha:(float)alpha
  310. {
  311.     [self setColor:NXConvertCMYKAToColor(cyan, magenta, yellow, black, alpha)];
  312.     return self;
  313. }
  314.  
  315. - (BOOL)isEqualColor:anObject
  316. {
  317.     if ([anObject respondsTo:@selector(actualColor)]) {
  318.         return NXEqualColor([self actualColor], [anObject actualColor]);
  319.     } else if ([anObject respondsTo:@selector(color)]) {
  320.         return NXEqualColor([self color], [anObject color]);
  321.     }
  322.     return NO;
  323. }
  324.  
  325. - (BOOL)isEqualToColor:(NXColor)color
  326. {
  327.     return NXEqualColor([self color], color);
  328. }
  329.  
  330. - (BOOL)isEqual:anObject
  331. {
  332.     if ([anObject class] != [self class]) return NO;
  333.     return [self isEqualColor:anObject];
  334. }
  335.  
  336. - read:(NXTypedStream *)stream
  337. {
  338.     [super read:stream];
  339.     [self setColor:NXReadColor(stream)];
  340.     [self setDelegate:NXReadObject(stream)];
  341.     return self;
  342. }
  343.  
  344. - write:(NXTypedStream *)stream
  345. {
  346.     [super write:stream];
  347.     NXWriteColor(stream, [self actualColor]);
  348.     NXWriteObject(stream, [self delegate]);
  349.     return self;
  350. }
  351.  
  352. // NXTransport protocol:  (I hope these will work, since I don't know another
  353. //        way to encode and pass a color through a portal!)
  354. - encodeUsing:(id <NXEncoding>)portal
  355. {
  356.     NXColor sendColor = [self actualColor];
  357.     id sendDelegate = [self delegate];
  358.     [portal encodeData:&sendColor ofType:@encode(NXColor)];
  359.     [portal encodeData:&sendDelegate ofType:"@"];
  360.     return self;
  361. }
  362.  
  363. - decodeUsing:(id <NXDecoding>)portal
  364. {
  365.     NXColor newColor;
  366.     id newDelegate;
  367.     [portal decodeData:&newColor ofType:@encode(NXColor)];
  368.     [portal decodeData:&newDelegate ofType:"@"];
  369.     [self setColor:newColor];
  370.     [self setDelegate:newDelegate];
  371.     return self;
  372. }
  373.  
  374. - encodeRemotelyFor:(NXConnection *)connection
  375.     freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
  376. {
  377.     if (isByCopy) {
  378.         *flagp = NO; // object will not be freed (ie, copy,  not move)
  379.         return self; //encode object (copy it)
  380.     }
  381.     *flagp = NO;
  382.     // super will encode the proxy otherwise
  383.     return [super encodeRemotelyFor:connection
  384.                 freeAfterEncoding:flagp isBycopy:isByCopy];
  385. }
  386.  
  387. // Interface Builder support
  388. - (const char *)getInspectorClassName { return "MiscColorInspector"; }
  389. - (NXImage *)getIBImage { return [NXImage findImageNamed:"MiscColorObj"]; }
  390.  
  391. - (float)redComponent { return NXRedComponent([self actualColor]); }
  392. - (float)greenComponent { return NXGreenComponent([self actualColor]); }
  393. - (float)blueComponent { return NXBlueComponent([self actualColor]); }
  394. - (float)cyanComponent { return NXCyanComponent([self actualColor]); }
  395. - (float)magentaComponent { return NXMagentaComponent([self actualColor]); }
  396. - (float)yellowComponent { return NXYellowComponent([self actualColor]); }
  397. - (float)blackComponent { return NXBlackComponent([self actualColor]); }
  398. - (float)hueComponent { return NXHueComponent([self actualColor]); }
  399. - (float)saturationComponent { return NXSaturationComponent([self actualColor]); }
  400. - (float)brightnessComponent { return NXBrightnessComponent([self actualColor]); }
  401. - (float)grayComponent { return NXGrayComponent([self actualColor]); }
  402. - (float)alphaComponent { return NXAlphaComponent([self actualColor]); }
  403.  
  404. + findColorNamed:(const char *)aName inList:(const char *)aListName
  405. {
  406.     NXColor newColor;
  407.     if (!NXFindColorNamed(aListName, aName, &newColor)) return nil;
  408.     return [[self alloc] initColor:newColor];
  409. }
  410.  
  411. - (const char *)colorName { return NXColorName([self actualColor]); }
  412. - (const char *)colorListName { return NXColorListName([self actualColor]); }
  413.  
  414. - delegate { return delegate; }
  415. - setDelegate:anObject
  416. {
  417.     id ret = delegate;
  418.     delegate = anObject;
  419.     return ret;
  420. }
  421.  
  422. @end
  423.  
  424.  
  425. // Examples of a NULL delegate
  426. @implementation MiscColor(delegate)
  427.  
  428. - (NXColor)willSendColorToPS:(NXColor)color shouldPrint:(BOOL *)printOK
  429. {
  430.     *printOK = YES;
  431.     return color;
  432. }
  433.  
  434. - didSendColorToPS:(NXColor)color
  435. {
  436.     return self;
  437. }
  438.  
  439. @end
  440.