Last active
August 29, 2015 14:23
-
-
Save pingguoilove/8cb324d8ce2c1e28f576 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * ColourUtils.h | |
| * | |
| * Copyright 2012 Stewart Hamilton-Arrandale. | |
| * http://creativewax.co.uk | |
| * | |
| * Modified by Yannick Loriot. | |
| * http://yannickloriot.com | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in | |
| * all copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| * THE SOFTWARE. | |
| * | |
| */ | |
| #import <Foundation/Foundation.h> | |
| typedef struct | |
| { | |
| double r; // percent | |
| double g; // percent | |
| double b; // percent | |
| double a; // percent | |
| } RGBA; | |
| typedef struct | |
| { | |
| double h; // angle in degrees | |
| double s; // percent | |
| double v; // percent | |
| } HSV; | |
| @interface CCColourUtils : NSObject | |
| #pragma mark - Constuctors - Initializers | |
| #pragma mark - Public Methods | |
| + (HSV)HSVfromRGB:(RGBA)value; | |
| + (RGBA)RGBfromHSV:(HSV)value; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * ColourUtils.m | |
| * | |
| * Copyright 2012 Stewart Hamilton-Arrandale. | |
| * http://creativewax.co.uk | |
| * | |
| * Modified by Yannick Loriot. | |
| * http://yannickloriot.com | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in | |
| * all copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| * THE SOFTWARE. | |
| * | |
| */ | |
| #import "CCColourUtils.h" | |
| @implementation CCColourUtils | |
| #pragma mark - Initialization | |
| #pragma mark - | |
| #pragma mark ColourUtils Public Methods | |
| + (HSV)HSVfromRGB:(RGBA)value | |
| { | |
| HSV out; | |
| double min, max, delta; | |
| min = value.r < value.g ? value.r : value.g; | |
| min = min < value.b ? min : value.b; | |
| max = value.r > value.g ? value.r : value.g; | |
| max = max > value.b ? max : value.b; | |
| out.v = max; // v | |
| delta = max - min; | |
| if( max > 0.0 ) | |
| { | |
| out.s = (delta / max); // s | |
| } else | |
| { | |
| // r = g = b = 0 // s = 0, v is undefined | |
| out.s = 0.0; | |
| out.h = NAN; // its now undefined | |
| return out; | |
| } | |
| if( value.r >= max ) // > is bogus, just keeps compilor happy | |
| { | |
| out.h = ( value.g - value.b ) / delta; // between yellow & magenta | |
| } else | |
| { | |
| if( value.g >= max ) | |
| out.h = 2.0 + ( value.b - value.r ) / delta; // between cyan & yellow | |
| else | |
| out.h = 4.0 + ( value.r - value.g ) / delta; // between magenta & cyan | |
| } | |
| out.h *= 60.0; // degrees | |
| if( out.h < 0.0 ) | |
| out.h += 360.0; | |
| return out; | |
| } | |
| + (RGBA)RGBfromHSV:(HSV)value | |
| { | |
| double hh, p, q, t, ff; | |
| long i; | |
| RGBA out; | |
| out.a = 1; | |
| if (value.s <= 0.0) // < is bogus, just shuts up warnings | |
| { | |
| if (isnan(value.h)) // value.h == NAN | |
| { | |
| out.r = value.v; | |
| out.g = value.v; | |
| out.b = value.v; | |
| return out; | |
| } | |
| // error - should never happen | |
| out.r = 0.0; | |
| out.g = 0.0; | |
| out.b = 0.0; | |
| return out; | |
| } | |
| hh = value.h; | |
| if(hh >= 360.0) hh = 0.0; | |
| hh /= 60.0; | |
| i = (long)hh; | |
| ff = hh - i; | |
| p = value.v * (1.0 - value.s); | |
| q = value.v * (1.0 - (value.s * ff)); | |
| t = value.v * (1.0 - (value.s * (1.0 - ff))); | |
| switch(i) | |
| { | |
| case 0: | |
| out.r = value.v; | |
| out.g = t; | |
| out.b = p; | |
| break; | |
| case 1: | |
| out.r = q; | |
| out.g = value.v; | |
| out.b = p; | |
| break; | |
| case 2: | |
| out.r = p; | |
| out.g = value.v; | |
| out.b = t; | |
| break; | |
| case 3: | |
| out.r = p; | |
| out.g = q; | |
| out.b = value.v; | |
| break; | |
| case 4: | |
| out.r = t; | |
| out.g = p; | |
| out.b = value.v; | |
| break; | |
| case 5: | |
| default: | |
| out.r = value.v; | |
| out.g = p; | |
| out.b = q; | |
| break; | |
| } | |
| return out; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment