aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlan Third2021-08-14 16:11:30 +0100
committerAlan Third2021-12-22 20:48:19 +0000
commit0f427befe82f88237bdccbd528baf76f6d6485b0 (patch)
treedffb5221ec905df3e78f147adbd4fced22e77118 /src
parent5b0121b708986c836fa970b800387363806a035a (diff)
downloademacs-0f427befe82f88237bdccbd528baf76f6d6485b0.tar.gz
emacs-0f427befe82f88237bdccbd528baf76f6d6485b0.zip
Simplify NS color handling
* src/image.c (RGB_TO_ULONG): (ARGB_TO_ULONG): (RED_FROM_ULONG): (RED16_FROM_ULONG): (GREEN16_FROM_ULONG): (BLUE16_FROM_ULONG): Define these here for NS too. * src/nsfns.m (ns_set_foreground_color): (ns_set_background_color): Use new EmacsColor methods. * src/nsterm.h (struct ns_color_table): Replace this struct with a built-in Objective C type. (RGB_TO_ULONG): (ARGB_TO_ULONG): (ALPHA_FROM_ULONG): (RED_FROM_ULONG): (GREEN_FROM_ULONG): (BLUE_FROM_ULONG): (RED16_FROM_ULONG): (GREEN16_FROM_ULONG): (BLUE16_FROM_ULONG): These are no longer needed in the NS specific code. (struct ns_display_info): Use an NSMutableArray instead of a custom struct. * src/nsterm.m ([NSColor colorWithUnsignedLong:hasAlpha:]): ([NSColor unsignedLong]): New methods. (ns_lookup_indexed_color): (ns_index_color): Use the NSMutableArray lookup table. (ns_term_init): (ns_color_index_to_rgba): (ns_query_color): Use the new EmacsColor methods. (ns_initialize_display_info): (ns_delete_display): Initialize and release the NSMutableArray lookup table.
Diffstat (limited to 'src')
-rw-r--r--src/image.c18
-rw-r--r--src/nsfns.m18
-rw-r--r--src/nsterm.h37
-rw-r--r--src/nsterm.m94
4 files changed, 60 insertions, 107 deletions
diff --git a/src/image.c b/src/image.c
index dd5ea19fc15..dc9aae27c92 100644
--- a/src/image.c
+++ b/src/image.c
@@ -80,14 +80,7 @@ typedef struct x_bitmap_record Bitmap_Record;
80#endif /* !USE_CAIRO */ 80#endif /* !USE_CAIRO */
81#endif /* HAVE_X_WINDOWS */ 81#endif /* HAVE_X_WINDOWS */
82 82
83#ifdef USE_CAIRO 83#if defined(USE_CAIRO) || defined(HAVE_NS)
84#define GET_PIXEL image_pix_context_get_pixel
85#define PUT_PIXEL image_pix_container_put_pixel
86#define NO_PIXMAP 0
87
88#define PIX_MASK_RETAIN 0
89#define PIX_MASK_DRAW 255
90
91#define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b)) 84#define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b))
92#define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) 85#define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
93#define RED_FROM_ULONG(color) (((color) >> 16) & 0xff) 86#define RED_FROM_ULONG(color) (((color) >> 16) & 0xff)
@@ -96,6 +89,15 @@ typedef struct x_bitmap_record Bitmap_Record;
96#define RED16_FROM_ULONG(color) (RED_FROM_ULONG (color) * 0x101) 89#define RED16_FROM_ULONG(color) (RED_FROM_ULONG (color) * 0x101)
97#define GREEN16_FROM_ULONG(color) (GREEN_FROM_ULONG (color) * 0x101) 90#define GREEN16_FROM_ULONG(color) (GREEN_FROM_ULONG (color) * 0x101)
98#define BLUE16_FROM_ULONG(color) (BLUE_FROM_ULONG (color) * 0x101) 91#define BLUE16_FROM_ULONG(color) (BLUE_FROM_ULONG (color) * 0x101)
92#endif
93
94#ifdef USE_CAIRO
95#define GET_PIXEL image_pix_context_get_pixel
96#define PUT_PIXEL image_pix_container_put_pixel
97#define NO_PIXMAP 0
98
99#define PIX_MASK_RETAIN 0
100#define PIX_MASK_DRAW 255
99 101
100static unsigned long image_alloc_image_color (struct frame *, struct image *, 102static unsigned long image_alloc_image_color (struct frame *, struct image *,
101 Lisp_Object, unsigned long); 103 Lisp_Object, unsigned long);
diff --git a/src/nsfns.m b/src/nsfns.m
index 81019fce09d..365df7fc1e8 100644
--- a/src/nsfns.m
+++ b/src/nsfns.m
@@ -236,7 +236,6 @@ static void
236ns_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval) 236ns_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
237{ 237{
238 NSColor *col; 238 NSColor *col;
239 EmacsCGFloat r, g, b, alpha;
240 239
241 /* Must block_input, because ns_lisp_to_color does block/unblock_input 240 /* Must block_input, because ns_lisp_to_color does block/unblock_input
242 which means that col may be deallocated in its unblock_input if there 241 which means that col may be deallocated in its unblock_input if there
@@ -253,12 +252,7 @@ ns_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
253 [f->output_data.ns->foreground_color release]; 252 [f->output_data.ns->foreground_color release];
254 f->output_data.ns->foreground_color = col; 253 f->output_data.ns->foreground_color = col;
255 254
256 [col getRed: &r green: &g blue: &b alpha: &alpha]; 255 FRAME_FOREGROUND_PIXEL (f) = [col unsignedLong];
257 FRAME_FOREGROUND_PIXEL (f) =
258 ARGB_TO_ULONG ((unsigned long) (alpha * 0xff),
259 (unsigned long) (r * 0xff),
260 (unsigned long) (g * 0xff),
261 (unsigned long) (b * 0xff));
262 256
263 if (FRAME_NS_VIEW (f)) 257 if (FRAME_NS_VIEW (f))
264 { 258 {
@@ -277,7 +271,7 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
277 struct face *face; 271 struct face *face;
278 NSColor *col; 272 NSColor *col;
279 NSView *view = FRAME_NS_VIEW (f); 273 NSView *view = FRAME_NS_VIEW (f);
280 EmacsCGFloat r, g, b, alpha; 274 EmacsCGFloat alpha;
281 275
282 block_input (); 276 block_input ();
283 if (ns_lisp_to_color (arg, &col)) 277 if (ns_lisp_to_color (arg, &col))
@@ -291,12 +285,8 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
291 [f->output_data.ns->background_color release]; 285 [f->output_data.ns->background_color release];
292 f->output_data.ns->background_color = col; 286 f->output_data.ns->background_color = col;
293 287
294 [col getRed: &r green: &g blue: &b alpha: &alpha]; 288 FRAME_BACKGROUND_PIXEL (f) = [col unsignedLong];
295 FRAME_BACKGROUND_PIXEL (f) = 289 alpha = [col alphaComponent];
296 ARGB_TO_ULONG ((unsigned long) (alpha * 0xff),
297 (unsigned long) (r * 0xff),
298 (unsigned long) (g * 0xff),
299 (unsigned long) (b * 0xff));
300 290
301 if (view != nil) 291 if (view != nil)
302 { 292 {
diff --git a/src/nsterm.h b/src/nsterm.h
index ce8f5949024..8bd7611441f 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -358,7 +358,7 @@ typedef id instancetype;
358+ (NSColor *)colorForEmacsRed:(CGFloat)red green:(CGFloat)green 358+ (NSColor *)colorForEmacsRed:(CGFloat)red green:(CGFloat)green
359 blue:(CGFloat)blue alpha:(CGFloat)alpha; 359 blue:(CGFloat)blue alpha:(CGFloat)alpha;
360- (NSColor *)colorUsingDefaultColorSpace; 360- (NSColor *)colorUsingDefaultColorSpace;
361 361- (unsigned long)unsignedLong;
362@end 362@end
363 363
364 364
@@ -766,35 +766,6 @@ struct ns_bitmap_record
766 int height, width, depth; 766 int height, width, depth;
767}; 767};
768 768
769/* This maps between emacs color indices and NSColor objects. */
770struct ns_color_table
771{
772 ptrdiff_t size;
773 ptrdiff_t avail;
774#ifdef __OBJC__
775 NSColor **colors;
776 NSMutableSet *empty_indices;
777#else
778 void **items;
779 void *availIndices;
780#endif
781};
782#define NS_COLOR_CAPACITY 256
783
784#define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b))
785#define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
786
787#define ALPHA_FROM_ULONG(color) ((color) >> 24)
788#define RED_FROM_ULONG(color) (((color) >> 16) & 0xff)
789#define GREEN_FROM_ULONG(color) (((color) >> 8) & 0xff)
790#define BLUE_FROM_ULONG(color) ((color) & 0xff)
791
792/* Do not change `* 0x101' in the following lines to `<< 8'. If
793 changed, image masks in 1-bit depth will not work. */
794#define RED16_FROM_ULONG(color) (RED_FROM_ULONG(color) * 0x101)
795#define GREEN16_FROM_ULONG(color) (GREEN_FROM_ULONG(color) * 0x101)
796#define BLUE16_FROM_ULONG(color) (BLUE_FROM_ULONG(color) * 0x101)
797
798#ifdef NS_IMPL_GNUSTEP 769#ifdef NS_IMPL_GNUSTEP
799/* this extends font backend font */ 770/* this extends font backend font */
800struct nsfont_info 771struct nsfont_info
@@ -850,7 +821,11 @@ struct ns_display_info
850 ptrdiff_t bitmaps_size; 821 ptrdiff_t bitmaps_size;
851 ptrdiff_t bitmaps_last; 822 ptrdiff_t bitmaps_last;
852 823
853 struct ns_color_table *color_table; 824#ifdef __OBJC__
825 NSMutableArray *color_table;
826#else
827 void *color_table;
828#endif
854 829
855 /* DPI resolution of this screen */ 830 /* DPI resolution of this screen */
856 double resx, resy; 831 double resx, resy;
diff --git a/src/nsterm.m b/src/nsterm.m
index 4e5ce5ef700..12f24fbfd53 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -166,6 +166,29 @@ char const * nstrace_fullscreen_type_name (int fs_type)
166 return [self colorUsingColorSpace: [NSColorSpace deviceRGBColorSpace]]; 166 return [self colorUsingColorSpace: [NSColorSpace deviceRGBColorSpace]];
167} 167}
168 168
169+ (NSColor *)colorWithUnsignedLong:(unsigned long)c
170 hasAlpha:(BOOL)alpha
171{
172 EmacsCGFloat a = (double)((c >> 24) & 0xff) / 255.0;
173 EmacsCGFloat r = (double)((c >> 16) & 0xff) / 255.0;
174 EmacsCGFloat g = (double)((c >> 8) & 0xff) / 255.0;
175 EmacsCGFloat b = (double)(c & 0xff) / 255.0;
176
177 return [NSColor colorForEmacsRed:r green:g blue:b
178 alpha:(alpha ? a : (EmacsCGFloat)1.0)];
179}
180
181- (unsigned long)unsignedLong
182{
183 EmacsCGFloat r, g, b, a;
184 [self getRed:&r green:&g blue:&b alpha:&a];
185
186 return (((unsigned long) (a * 255)) << 24)
187 | (((unsigned long) (r * 255)) << 16)
188 | (((unsigned long) (g * 255)) << 8)
189 | ((unsigned long) (b * 255));
190}
191
169@end 192@end
170 193
171/* ========================================================================== 194/* ==========================================================================
@@ -1952,53 +1975,29 @@ ns_fullscreen_hook (struct frame *f)
1952NSColor * 1975NSColor *
1953ns_lookup_indexed_color (unsigned long idx, struct frame *f) 1976ns_lookup_indexed_color (unsigned long idx, struct frame *f)
1954{ 1977{
1955 struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table; 1978 NSMutableArray *color_table = FRAME_DISPLAY_INFO (f)->color_table;
1956 if (idx < 1 || idx >= color_table->avail) 1979 if (idx < 1 || idx >= [color_table count])
1957 return nil; 1980 return nil;
1958 return color_table->colors[idx]; 1981 return [color_table objectAtIndex:idx];
1959} 1982}
1960 1983
1961 1984
1962unsigned long 1985unsigned long
1963ns_index_color (NSColor *color, struct frame *f) 1986ns_index_color (NSColor *color, struct frame *f)
1964{ 1987{
1965 struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table; 1988 NSMutableArray *color_table = FRAME_DISPLAY_INFO (f)->color_table;
1966 ptrdiff_t idx;
1967 ptrdiff_t i;
1968 1989
1969 if (!color_table->colors) 1990 /* An index of 0 appears to be special in some way, so insert a
1970 { 1991 dummy object. */
1971 color_table->size = NS_COLOR_CAPACITY; 1992 if ([color_table count] == 0)
1972 color_table->avail = 1; /* skip idx=0 as marker */ 1993 [color_table addObject:[NSNull null]];
1973 color_table->colors = xmalloc (color_table->size * sizeof (NSColor *));
1974 color_table->colors[0] = nil;
1975 color_table->empty_indices = [[NSMutableSet alloc] init];
1976 }
1977 1994
1978 /* Do we already have this color? */ 1995 /* Do we already have this color? */
1979 for (i = 1; i < color_table->avail; i++) 1996 if ([color_table containsObject:color])
1980 if (color_table->colors[i] && [color_table->colors[i] isEqual: color]) 1997 return [color_table indexOfObject:color];
1981 return i;
1982
1983 if ([color_table->empty_indices count] > 0)
1984 {
1985 NSNumber *index = [color_table->empty_indices anyObject];
1986 [color_table->empty_indices removeObject: index];
1987 idx = [index unsignedLongValue];
1988 }
1989 else
1990 {
1991 if (color_table->avail == color_table->size)
1992 color_table->colors =
1993 xpalloc (color_table->colors, &color_table->size, 1,
1994 min (ULONG_MAX, PTRDIFF_MAX), sizeof *color_table->colors);
1995 idx = color_table->avail++;
1996 }
1997 1998
1998 color_table->colors[idx] = color; 1999 [color_table addObject:color];
1999 [color retain]; 2000 return [color_table count] - 1;
2000 /* fprintf(stderr, "color_table: allocated %d\n",idx); */
2001 return idx;
2002} 2001}
2003 2002
2004 2003
@@ -2136,13 +2135,7 @@ ns_color_index_to_rgba(int idx, struct frame *f)
2136 NSColor *col; 2135 NSColor *col;
2137 col = ns_lookup_indexed_color (idx, f); 2136 col = ns_lookup_indexed_color (idx, f);
2138 2137
2139 EmacsCGFloat r, g, b, a; 2138 return [col unsignedLong];
2140 [col getRed: &r green: &g blue: &b alpha: &a];
2141
2142 return ARGB_TO_ULONG((unsigned long) (a * 255),
2143 (unsigned long) (r * 255),
2144 (unsigned long) (g * 255),
2145 (unsigned long) (b * 255));
2146} 2139}
2147 2140
2148void 2141void
@@ -2161,11 +2154,7 @@ ns_query_color(void *col, Emacs_Color *color_def, bool setPixel)
2161 color_def->blue = b * 65535; 2154 color_def->blue = b * 65535;
2162 2155
2163 if (setPixel == YES) 2156 if (setPixel == YES)
2164 color_def->pixel 2157 color_def->pixel = [(NSColor *)col unsignedLong];
2165 = ARGB_TO_ULONG((unsigned long) (a * 255),
2166 (unsigned long) (r * 255),
2167 (unsigned long) (g * 255),
2168 (unsigned long) (b * 255));
2169} 2158}
2170 2159
2171bool 2160bool
@@ -4933,8 +4922,7 @@ ns_initialize_display_info (struct ns_display_info *dpyinfo)
4933 && ![NSCalibratedWhiteColorSpace isEqualToString: 4922 && ![NSCalibratedWhiteColorSpace isEqualToString:
4934 NSColorSpaceFromDepth (depth)]; 4923 NSColorSpaceFromDepth (depth)];
4935 dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth); 4924 dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth);
4936 dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table); 4925 dpyinfo->color_table = [[NSMutableArray array] retain];
4937 dpyinfo->color_table->colors = NULL;
4938 dpyinfo->root_window = 42; /* A placeholder. */ 4926 dpyinfo->root_window = 42; /* A placeholder. */
4939 dpyinfo->highlight_frame = dpyinfo->ns_focus_frame = NULL; 4927 dpyinfo->highlight_frame = dpyinfo->ns_focus_frame = NULL;
4940 dpyinfo->n_fonts = 0; 4928 dpyinfo->n_fonts = 0;
@@ -5010,6 +4998,7 @@ static void
5010ns_delete_display (struct ns_display_info *dpyinfo) 4998ns_delete_display (struct ns_display_info *dpyinfo)
5011{ 4999{
5012 /* TODO... */ 5000 /* TODO... */
5001 [dpyinfo->color_table release];
5013} 5002}
5014 5003
5015 5004
@@ -5223,10 +5212,7 @@ ns_term_init (Lisp_Object display_name)
5223 name = XCAR (color); 5212 name = XCAR (color);
5224 c = XFIXNUM (XCDR (color)); 5213 c = XFIXNUM (XCDR (color));
5225 [cl setColor: 5214 [cl setColor:
5226 [NSColor colorForEmacsRed: RED_FROM_ULONG (c) / 255.0 5215 [NSColor colorWithUnsignedLong:c hasAlpha:NO]
5227 green: GREEN_FROM_ULONG (c) / 255.0
5228 blue: BLUE_FROM_ULONG (c) / 255.0
5229 alpha: 1.0]
5230 forKey: [NSString stringWithLispString: name]]; 5216 forKey: [NSString stringWithLispString: name]];
5231 } 5217 }
5232 5218