diff --git a/include/wx/osx/core/colour.h b/include/wx/osx/core/colour.h index 228d10c7c7..679bb9d667 100644 --- a/include/wx/osx/core/colour.h +++ b/include/wx/osx/core/colour.h @@ -58,7 +58,8 @@ public: wxColour& operator=(const RGBColor& col); #endif #if wxOSX_USE_COCOA - wxColour(WX_NSColor color); + // This ctor does not take ownership of the color. + explicit wxColour(WX_NSColor color); WX_NSColor OSXGetNSColor() const; #endif wxColour& operator=(CGColorRef col); diff --git a/src/osx/carbon/colordlgosx.mm b/src/osx/carbon/colordlgosx.mm index eb51e1d755..9d3fddd0e2 100644 --- a/src/osx/carbon/colordlgosx.mm +++ b/src/osx/carbon/colordlgosx.mm @@ -157,14 +157,7 @@ int wxColourDialog::ShowModal() [theCPDelegate release]; //Get the shared color panel along with the chosen color and set the chosen color - NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; - - m_colourData.GetColour().Set( - (unsigned char) ([theColor redComponent] * 255.0), - (unsigned char) ([theColor greenComponent] * 255.0), - (unsigned char) ([theColor blueComponent] * 255.0), - (unsigned char) ([theColor alphaComponent] * 255.0) - ); + m_colourData.GetColour() = wxColour([theColorPanel color]); //Release the pool, we're done :) [thePool release]; diff --git a/src/osx/carbon/fontdlgosx.mm b/src/osx/carbon/fontdlgosx.mm index dc4ac39ccd..324c60d544 100644 --- a/src/osx/carbon/fontdlgosx.mm +++ b/src/osx/carbon/fontdlgosx.mm @@ -208,11 +208,7 @@ int RunMixedFontDialog(wxFontDialog* dialog) fontdata.m_chosenFont = wxFont( theFont ); //Get the shared color panel along with the chosen color and set the chosen color - NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; - - fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0), - (unsigned char) ([theColor greenComponent] * 255.0), - (unsigned char) ([theColor blueComponent] * 255.0)); + fontdata.m_fontColour = wxColour([[NSColorPanel sharedColorPanel] color]); #endif retval = wxID_OK ; } @@ -525,11 +521,7 @@ int wxFontDialog::ShowModal() theFontWeight >= 9 ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL); //Get the shared color panel along with the chosen color and set the chosen color - NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; - - m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0), - (unsigned char) ([theColor greenComponent] * 255.0), - (unsigned char) ([theColor blueComponent] * 255.0)); + m_fontData.m_fontColour = wxColour([theColorPanel color]); //Friendly debug stuff #ifdef FONTDLGDEBUG diff --git a/src/osx/cocoa/colour.mm b/src/osx/cocoa/colour.mm index 73df7fbf01..b47293b692 100644 --- a/src/osx/cocoa/colour.mm +++ b/src/osx/cocoa/colour.mm @@ -1,10 +1,9 @@ ///////////////////////////////////////////////////////////////////////////// // Name: src/osx/cocoa/colour.mm -// Purpose: Cocoa additions to wxColour class -// Author: Kevin Ollivier -// Modified by: -// Created: 2009-10-31 -// Copyright: (c) Kevin Ollivier +// Purpose: Conversions between NSColor and wxColour +// Author: Vadim Zeitlin +// Created: 2015-11-26 (completely replacing the old version of the file) +// Copyright: (c) 2015 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -12,31 +11,34 @@ #include "wx/colour.h" -#ifndef WX_PRECOMP - #include "wx/gdicmn.h" -#endif - #include "wx/osx/private.h" +// Helper function to avoid writing too many casts in wxColour ctor. +static inline wxColour::ChannelType NSColorChannelToWX(CGFloat c) +{ + return static_cast(c * 255 + 0.5); +} + wxColour::wxColour(WX_NSColor col) { - size_t noComp = [col numberOfComponents]; - - CGFloat components[4]; - CGFloat *p; - if ( noComp < 1 || noComp > WXSIZEOF(components) ) + // Simplest case is when we can directly get the RGBA components: + if ( NSColor* colRGBA = [col colorUsingColorSpaceName:NSCalibratedRGBColorSpace] ) { - // TODO verify whether we really are on a RGB color space - m_alpha = wxALPHA_OPAQUE; - [col getComponents: components]; - p = components; - } - else // Unsupported colour format. - { - p = NULL; + InitRGBA + ( + NSColorChannelToWX([colRGBA redComponent]), + NSColorChannelToWX([colRGBA greenComponent]), + NSColorChannelToWX([colRGBA blueComponent]), + NSColorChannelToWX([colRGBA alphaComponent]) + ); + return; } - InitFromComponents(components, noComp); + // Don't assert here, this will more likely than not result in a crash as + // colours are often created in drawing code which will be called again + // when the assert dialog is shown, resulting in a recursive assertion + // failure and, hence, a crash. + NSLog(@"Failed to convert NSColor \"%@\" to wxColour.", col); } WX_NSColor wxColour::OSXGetNSColor() const