ObjFW  Check-in [061e16d166]

Overview
Comment:OFColor: Allow some imprecision for tagged pointer
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 061e16d166f1cb308ded399848ac45246a2c3914d7b13c5fa3942b0555caa1aa
User & Date: js on 2023-02-13 22:14:16
Other Links: manifest | tags
Context
2023-02-15
21:54
OFMatrix4x4: Add -[transformedVector3D:] check-in: 1e550fabe3 user: js tags: trunk
2023-02-13
22:14
OFColor: Allow some imprecision for tagged pointer check-in: 061e16d166 user: js tags: trunk
20:32
Support for storing OFColors in tagged pointers check-in: 51c8047121 user: js tags: trunk
Changes

Modified src/OFColor.m from [668849c2be] to [2f23670cfa].

10
11
12
13
14
15
16


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


37
38
39
40
41
42
43
 * 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 "OFOnce.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"

@interface OFColor ()
+ (instancetype)of_alloc;
@end

@interface OFColorSingleton: OFColor
@end

@interface OFColorPlaceholder: OFColorSingleton
@end

#ifdef OF_OBJFW_RUNTIME
@interface OFTaggedPointerColor: OFColorSingleton
@end


#endif

static struct {
	Class isa;
} placeholder;

#ifdef OF_OBJFW_RUNTIME







>
>




















>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 * 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"

#include <math.h>

#import "OFColor.h"
#import "OFOnce.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"

@interface OFColor ()
+ (instancetype)of_alloc;
@end

@interface OFColorSingleton: OFColor
@end

@interface OFColorPlaceholder: OFColorSingleton
@end

#ifdef OF_OBJFW_RUNTIME
@interface OFTaggedPointerColor: OFColorSingleton
@end

static const float allowedImprecision = 0.0000001;
#endif

static struct {
	Class isa;
} placeholder;

#ifdef OF_OBJFW_RUNTIME
68
69
70
71
72
73
74
75
76
77
78

79
80
81
82
83
84
85
86
87
@implementation OFColorPlaceholder
- (instancetype)initWithRed: (float)red
		      green: (float)green
		       blue: (float)blue
		      alpha: (float)alpha
{
#ifdef OF_OBJFW_RUNTIME
	uint8_t redInt = red * 255;
	uint8_t greenInt = green * 255;
	uint8_t blueInt = blue * 255;


	if (red * 255 == redInt && green * 255 == greenInt &&
	    blue * 255 == blueInt && alpha == 1) {
		id ret = objc_createTaggedPointer(colorTag,
		    (uintptr_t)redInt << 16 | (uintptr_t)greenInt << 8 |
		    (uintptr_t)blueInt);

		if (ret != nil)
			return ret;
	}







|
|
|

>
|
|







72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@implementation OFColorPlaceholder
- (instancetype)initWithRed: (float)red
		      green: (float)green
		       blue: (float)blue
		      alpha: (float)alpha
{
#ifdef OF_OBJFW_RUNTIME
	uint8_t redInt = nearbyintf(red * 255);
	uint8_t greenInt = nearbyintf(green * 255);
	uint8_t blueInt = nearbyintf(blue * 255);

	if (fabsf(red * 255 - redInt) < allowedImprecision &&
	    fabsf(green * 255 - greenInt) < allowedImprecision &&
	    fabsf(blue * 255 - blueInt) < allowedImprecision && alpha == 1) {
		id ret = objc_createTaggedPointer(colorTag,
		    (uintptr_t)redInt << 16 | (uintptr_t)greenInt << 8 |
		    (uintptr_t)blueInt);

		if (ret != nil)
			return ret;
	}
282
283
284
285
286
287
288
289
290
291
292
- (OFString *)description
{
	float red, green, blue, alpha;

	[self getRed: &red green: &green blue: &blue alpha: &alpha];

	return [OFString stringWithFormat:
	    @"<OFColor red=%f green=%f blue=%f alpha=%f>",
	    red, green, blue, alpha];
}
@end







|
|


287
288
289
290
291
292
293
294
295
296
297
- (OFString *)description
{
	float red, green, blue, alpha;

	[self getRed: &red green: &green blue: &blue alpha: &alpha];

	return [OFString stringWithFormat:
	    @"<%@ red=%f green=%f blue=%f alpha=%f>",
	    self.class, red, green, blue, alpha];
}
@end