Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -13,10 +13,11 @@ SRCS = OFApplication.m \ OFArray.m \ OFAutoreleasePool.m \ OFBlock.m \ OFCharacterSet.m \ + OFColor.m \ OFConstantString.m \ OFCountedSet.m \ OFData.m \ OFData+CryptoHashing.m \ OFData+MessagePackValue.m \ ADDED src/OFColor.h Index: src/OFColor.h ================================================================== --- src/OFColor.h +++ src/OFColor.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFObject.h" + +OF_ASSUME_NONNULL_BEGIN + +/** + * @class OFColor OFColor.h ObjFW/OFColor.h + * + * @brief A class for storing a color. + */ +@interface OFColor: OFObject +{ + float _red, _green, _blue, _alpha; +} + +/*! + * @brief Creates a new color with the specified red, green, blue and alpha + * value. + * + * @param red The red value of the color, between 0.0 and 1.0 + * @param green The green value of the color, between 0.0 and 1.0 + * @param blue The blue value of the color, between 0.0 and 1.0 + * @param alpha The alpha value of the color, between 0.0 and 1.0 + * @return A new color with the specified red, green, blue and alpha value + */ ++ (instancetype)colorWithRed: (float)red + green: (float)green + blue: (float)blue + alpha: (float)alpha; + +/*! + * @brief Initializes an already allocated color with the specified red, green, + * blue and alpha value. + * + * @param red The red value of the color, between 0.0 and 1.0 + * @param green The green value of the color, between 0.0 and 1.0 + * @param blue The blue value of the color, between 0.0 and 1.0 + * @param alpha The alpha value of the color, between 0.0 and 1.0 + * @return A color initialized with the specified red, green, blue and alpha + * value + */ +- (instancetype)initWithRed: (float)red + green: (float)green + blue: (float)blue + alpha: (float)alpha; + +/*! + * @brief Returns the red, green, blue and alpha value of the color. + * + * @param red A pointer to store the red value of the color + * @param green A pointer to store the green value of the color + * @param blue A pointer to store the blue value of the color + * @param alpha An optional pointer to store the alpha of the color + */ +- (void)getRed: (float *)red + green: (float *)green + blue: (float *)blue + alpha: (nullable float *)alpha; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFColor.m Index: src/OFColor.m ================================================================== --- src/OFColor.m +++ src/OFColor.m @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFColor.h" + +#import "OFInvalidArgumentException.h" + +@implementation OFColor ++ (instancetype)colorWithRed: (float)red + green: (float)green + blue: (float)blue + alpha: (float)alpha +{ + return [[[self alloc] initWithRed: red + green: green + blue: blue + alpha: alpha] autorelease]; +} + +- (instancetype)initWithRed: (float)red + green: (float)green + blue: (float)blue + alpha: (float)alpha +{ + self = [super init]; + + @try { + if (red < 0.0 || red > 1.0 || + green < 0.0 || green > 1.0 || + blue < 0.0 || blue > 1.0 || + alpha < 0.0 || alpha > 1.0) + @throw [OFInvalidArgumentException exception]; + + _red = red; + _green = green; + _blue = blue; + _alpha = alpha; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (bool)isEqual: (id)object +{ + OFColor *other; + + if (![object isKindOfClass: [OFColor class]]) + return false; + + other = object; + + if (other->_red != _red) + return false; + if (other->_green != _green) + return false; + if (other->_blue != _blue) + return false; + if (other->_alpha != _alpha) + return false; + + return true; +} + +- (uint32_t)hash +{ + uint32_t hash; + union { + float f; + unsigned char b[sizeof(float)]; + } f; + + OF_HASH_INIT(hash); + + f.f = OF_BSWAP_FLOAT_IF_LE(_red); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, f.b[i]); + + f.f = OF_BSWAP_FLOAT_IF_LE(_green); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, f.b[i]); + + f.f = OF_BSWAP_FLOAT_IF_LE(_blue); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, f.b[i]); + + f.f = OF_BSWAP_FLOAT_IF_LE(_alpha); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, f.b[i]); + + OF_HASH_FINALIZE(hash); + + return hash; +} + +- (void)getRed: (float *)red + green: (float *)green + blue: (float *)blue + alpha: (float *)alpha +{ + *red = _red; + *green = _green; + *blue = _blue; + + if (alpha != NULL) + *alpha = _alpha; +} +@end Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -1084,11 +1084,11 @@ if (isnan([self doubleValue])) return 0; d.d = OF_BSWAP_DOUBLE_IF_BE([self doubleValue]); - for (uint8_t i = 0; i < sizeof(double); i++) + for (uint_fast8_t i = 0; i < sizeof(double); i++) OF_HASH_ADD(hash, d.b[i]); } else if (type & OF_NUMBER_TYPE_SIGNED) { intmax_t v = [self intMaxValue] * -1; while (v != 0) { Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -48,10 +48,11 @@ #import "OFNumber.h" #import "OFDate.h" #import "OFURL.h" #import "OFURLHandler.h" +#import "OFColor.h" #import "OFStream.h" #import "OFStdIOStream.h" #import "OFInflateStream.h" #import "OFInflate64Stream.h"