mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
AppKit: Add bundle asset catalogs and named colours
Read bundle asset catalog from Resources/Assets.json, which Info.plist key CPAssetCatalog names. Resolve asset colours and images on use against drawing appearance. * _CPAssetCatalog.j: Add _CPAssetCatalog; load and cache catalog per bundle, select entries by appearance (exact luminosity and contrast, then same luminosity at normal contrast, then any luminosity), read colour components in Xcode notations, convert gray, linear and Display P3 values to sRGB, resolve System catalog references, select image scale by devicePixelRatio. * CPColor.j: - Extract _CPDynamicColor from _CPCatalogColor; keep resolution, forwarding and alpha override in base class. - Add _CPAssetColor; take value from catalog entry for drawing appearance; archive by name and bundle path. - Add +colorNamed: and +colorNamed:bundle:. * CPImage.j: - Extract _CPDynamicImage from _CPSystemImage; keep forwarding, size override and load notification in base class. - Add _CPAssetImage; take image from catalog entry for drawing appearance and display scale; archive by name and bundle path. - Add _CPScaledImage; derive point size from pixel size and scale. - Look up main bundle asset catalog in +imageNamed: after registered images and before system images. * AppKit.j: Import _CPAssetCatalog.j. * CPAssetCatalogTest.j: Add tests for colour and image lookup, appearance fallback, component notations, colour space conversion, idiom preference, scale selection and archiving.
This commit is contained in:
1 parent
dd8ead0d84
commit
36b43e6673
5 files changed
+1156
-62
No files matched your search
@@ -20,6 +20,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import "_CPAssetCatalog.j"
|
||||
@import "_CPObject+Theme.j"
|
||||
@import "_CPToolTip.j"
|
||||
@import "CALayer.j"
|
||||
|
||||
+197
-33
@@ -29,6 +29,9 @@
|
||||
@import "CPAppearance.j"
|
||||
@import "CPCompatibility.j"
|
||||
@import "CPImage.j"
|
||||
@import "_CPAssetCatalog.j"
|
||||
|
||||
@global _CPRaiseInvalidAbstractInvocation
|
||||
|
||||
|
||||
/*!
|
||||
@@ -637,6 +640,32 @@ function _CPSystemColorValue(aName, anAppearance)
|
||||
return color;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the colour named \c aName in the main bundle's asset catalog, or \c nil if there
|
||||
is none (see +colorNamed:bundle:).
|
||||
@param aName the name of the colour
|
||||
@return the named colour, or \c nil
|
||||
*/
|
||||
+ (CPColor)colorNamed:(CPString)aName
|
||||
{
|
||||
return [self colorNamed:aName bundle:nil];
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the colour named \c aName in the asset catalog of \c aBundle, or \c nil if there
|
||||
is none. A \c nil bundle means the main bundle. The asset catalog comes from the
|
||||
bundle's \c .xcassets directories, which the build compiles. An asset colour keeps its
|
||||
name and resolves to the catalog's value for the current drawing appearance (see
|
||||
+[CPAppearance currentDrawingAppearance]) whenever it is used.
|
||||
@param aName the name of the colour
|
||||
@param aBundle the bundle that holds the asset catalog, or \c nil for the main bundle
|
||||
@return the named colour, or \c nil
|
||||
*/
|
||||
+ (CPColor)colorNamed:(CPString)aName bundle:(CPBundle)aBundle
|
||||
{
|
||||
return [[_CPAssetCatalog catalogForBundle:aBundle] colorNamed:aName];
|
||||
}
|
||||
|
||||
/*
|
||||
The system colours. Each method returns the colour of that name in the System catalog,
|
||||
which resolves against the current theme and the current drawing appearance (see
|
||||
@@ -1598,7 +1627,9 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
CPColorCssAfterDictionaryKey = @"CPColorCssAfterDictionaryKey",
|
||||
CPColorCatalogNameKey = @"CPColorCatalogNameKey",
|
||||
CPColorNameKey = @"CPColorNameKey",
|
||||
CPColorCatalogAlphaKey = @"CPColorCatalogAlphaKey";
|
||||
CPColorCatalogAlphaKey = @"CPColorCatalogAlphaKey",
|
||||
CPColorAssetNameKey = @"CPColorAssetNameKey",
|
||||
CPColorAssetBundlePathKey = @"CPColorAssetBundlePathKey";
|
||||
/// @endcond
|
||||
|
||||
@implementation CPColor (CPCoding)
|
||||
@@ -1620,6 +1651,18 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
return catalogColor;
|
||||
}
|
||||
|
||||
if ([aCoder containsValueForKey:CPColorAssetNameKey])
|
||||
{
|
||||
var bundlePath = [aCoder decodeObjectForKey:CPColorAssetBundlePathKey],
|
||||
bundle = bundlePath ? [CPBundle bundleWithPath:bundlePath] : [CPBundle mainBundle],
|
||||
assetColor = [CPColor colorNamed:[aCoder decodeObjectForKey:CPColorAssetNameKey] bundle:bundle];
|
||||
|
||||
if (assetColor && [aCoder containsValueForKey:CPColorCatalogAlphaKey])
|
||||
assetColor = [assetColor colorWithAlphaComponent:[aCoder decodeFloatForKey:CPColorCatalogAlphaKey]];
|
||||
|
||||
return assetColor;
|
||||
}
|
||||
|
||||
if ([aCoder containsValueForKey:CPColorPatternImageKey])
|
||||
self = [self _initWithPatternImage:[aCoder decodeObjectForKey:CPColorPatternImageKey]];
|
||||
else if ([aCoder containsValueForKey:CPColorCssDictionaryKey])
|
||||
@@ -1658,36 +1701,47 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
|
||||
|
||||
/*
|
||||
A colour of a catalog, such as the System catalog. It keeps its catalog name and colour
|
||||
name and resolves to a value whenever it is used, so that it follows the current theme and
|
||||
the current drawing appearance. Every method that reads the colour's value reads the
|
||||
resolved colour. A colour made with -colorWithAlphaComponent: stays a catalog colour and
|
||||
replaces the alpha of the resolved colour.
|
||||
A colour whose value depends on the drawing appearance. It holds no value of its own and
|
||||
resolves to a colour whenever it is used (see -_resolvedColor), so that it follows the
|
||||
current theme and the current drawing appearance. Every method that reads the colour's
|
||||
value reads the resolved colour. A colour made with -colorWithAlphaComponent: stays
|
||||
dynamic and replaces the alpha of the resolved colour. Subclasses implement
|
||||
-_valueInAppearance: and -_dynamicCopy.
|
||||
*/
|
||||
@implementation _CPCatalogColor : CPColor
|
||||
@implementation _CPDynamicColor : CPColor
|
||||
{
|
||||
CPString _catalogName;
|
||||
CPString _colorName;
|
||||
float _alphaOverride;
|
||||
}
|
||||
|
||||
- (id)_initWithCatalogName:(CPString)aCatalogName colorName:(CPString)aColorName
|
||||
- (id)_initDynamicColor
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_catalogName = aCatalogName;
|
||||
_colorName = aColorName;
|
||||
_alphaOverride = nil;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the colour's value in anAppearance, before any alpha override.
|
||||
*/
|
||||
- (CPColor)_valueInAppearance:(CPAppearance)anAppearance
|
||||
{
|
||||
_CPRaiseInvalidAbstractInvocation(self, _cmd);
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a new colour of the receiver's kind and name, without alpha override.
|
||||
*/
|
||||
- (_CPDynamicColor)_dynamicCopy
|
||||
{
|
||||
_CPRaiseInvalidAbstractInvocation(self, _cmd);
|
||||
}
|
||||
|
||||
- (CPColor)_resolvedColor
|
||||
{
|
||||
var color = _CPSystemColorValue(_colorName, [CPAppearance currentDrawingAppearance]);
|
||||
var color = [self _valueInAppearance:[CPAppearance currentDrawingAppearance]];
|
||||
|
||||
if (_alphaOverride !== nil)
|
||||
color = [color colorWithAlphaComponent:_alphaOverride];
|
||||
@@ -1695,16 +1749,6 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
return color;
|
||||
}
|
||||
|
||||
- (CPString)catalogNameComponent
|
||||
{
|
||||
return _catalogName;
|
||||
}
|
||||
|
||||
- (CPString)colorNameComponent
|
||||
{
|
||||
return _colorName;
|
||||
}
|
||||
|
||||
- (CPImage)patternImage
|
||||
{
|
||||
return [[self _resolvedColor] patternImage];
|
||||
@@ -1747,7 +1791,7 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
|
||||
- (CPColor)colorWithAlphaComponent:(float)anAlphaComponent
|
||||
{
|
||||
var color = [[_CPCatalogColor alloc] _initWithCatalogName:_catalogName colorName:_colorName];
|
||||
var color = [self _dynamicCopy];
|
||||
|
||||
color._alphaOverride = anAlphaComponent;
|
||||
|
||||
@@ -1815,6 +1859,63 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
[[self _resolvedColor] setStroke];
|
||||
}
|
||||
|
||||
- (Class)classForKeyedArchiver
|
||||
{
|
||||
return [CPColor class];
|
||||
}
|
||||
|
||||
- (void)_encodeAlphaOverrideWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
if (_alphaOverride !== nil)
|
||||
[aCoder encodeFloat:_alphaOverride forKey:CPColorCatalogAlphaKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
A colour of a catalog, such as the System catalog. It keeps its catalog name and colour
|
||||
name and takes its value from the current theme (see _CPSystemColorValue).
|
||||
*/
|
||||
@implementation _CPCatalogColor : _CPDynamicColor
|
||||
{
|
||||
CPString _catalogName;
|
||||
CPString _colorName;
|
||||
}
|
||||
|
||||
- (id)_initWithCatalogName:(CPString)aCatalogName colorName:(CPString)aColorName
|
||||
{
|
||||
self = [super _initDynamicColor];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_catalogName = aCatalogName;
|
||||
_colorName = aColorName;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (CPColor)_valueInAppearance:(CPAppearance)anAppearance
|
||||
{
|
||||
return _CPSystemColorValue(_colorName, anAppearance);
|
||||
}
|
||||
|
||||
- (_CPDynamicColor)_dynamicCopy
|
||||
{
|
||||
return [[_CPCatalogColor alloc] _initWithCatalogName:_catalogName colorName:_colorName];
|
||||
}
|
||||
|
||||
- (CPString)catalogNameComponent
|
||||
{
|
||||
return _catalogName;
|
||||
}
|
||||
|
||||
- (CPString)colorNameComponent
|
||||
{
|
||||
return _colorName;
|
||||
}
|
||||
|
||||
/*
|
||||
Two catalog colours are equal when they name the same colour with the same alpha. A
|
||||
catalog colour and a colour of fixed value are equal when the catalog colour resolves
|
||||
@@ -1836,18 +1937,81 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
|
||||
return @"Catalog color: " + _catalogName + @" " + _colorName + (_alphaOverride !== nil ? @", alpha " + _alphaOverride : @"");
|
||||
}
|
||||
|
||||
- (Class)classForKeyedArchiver
|
||||
{
|
||||
return [CPColor class];
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[aCoder encodeObject:_catalogName forKey:CPColorCatalogNameKey];
|
||||
[aCoder encodeObject:_colorName forKey:CPColorNameKey];
|
||||
[self _encodeAlphaOverrideWithCoder:aCoder];
|
||||
}
|
||||
|
||||
if (_alphaOverride !== nil)
|
||||
[aCoder encodeFloat:_alphaOverride forKey:CPColorCatalogAlphaKey];
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
A colour of a bundle's asset catalog (see +colorNamed:bundle:). It keeps its name and
|
||||
catalog and takes its value from the catalog's entry for the appearance. It is archived by
|
||||
its name and, outside the main bundle, its bundle path.
|
||||
*/
|
||||
@implementation _CPAssetColor : _CPDynamicColor
|
||||
{
|
||||
CPString _assetName;
|
||||
_CPAssetCatalog _catalog;
|
||||
}
|
||||
|
||||
- (id)_initWithAssetName:(CPString)aName catalog:(_CPAssetCatalog)aCatalog
|
||||
{
|
||||
self = [super _initDynamicColor];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_assetName = aName;
|
||||
_catalog = aCatalog;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (CPColor)_valueInAppearance:(CPAppearance)anAppearance
|
||||
{
|
||||
return [_catalog _colorValueForAssetNamed:_assetName appearance:anAppearance];
|
||||
}
|
||||
|
||||
- (_CPDynamicColor)_dynamicCopy
|
||||
{
|
||||
return [[_CPAssetColor alloc] _initWithAssetName:_assetName catalog:_catalog];
|
||||
}
|
||||
|
||||
/*
|
||||
Two asset colours are equal when they name the same asset of the same catalog with the
|
||||
same alpha. An asset colour and a colour of fixed value are equal when the asset colour
|
||||
resolves to that value.
|
||||
*/
|
||||
- (BOOL)isEqual:(CPColor)aColor
|
||||
{
|
||||
if (aColor === self)
|
||||
return YES;
|
||||
|
||||
if ([aColor isKindOfClass:[_CPAssetColor class]])
|
||||
return _assetName === aColor._assetName && _catalog === aColor._catalog && _alphaOverride === aColor._alphaOverride;
|
||||
|
||||
return [[self _resolvedColor] isEqual:aColor];
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return @"Asset color: " + _assetName + (_alphaOverride !== nil ? @", alpha " + _alphaOverride : @"");
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
var bundle = [_catalog bundle];
|
||||
|
||||
[aCoder encodeObject:_assetName forKey:CPColorAssetNameKey];
|
||||
|
||||
if (bundle !== [CPBundle mainBundle])
|
||||
[aCoder encodeObject:[bundle bundlePath] forKey:CPColorAssetBundlePathKey];
|
||||
|
||||
[self _encodeAlphaOverrideWithCoder:aCoder];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
+139
-29
@@ -36,9 +36,12 @@
|
||||
@import "CPGraphicsContext.j"
|
||||
@import "CPTheme.j"
|
||||
|
||||
@import "_CPAssetCatalog.j"
|
||||
|
||||
@class CPColor
|
||||
@class _CPSystemImage
|
||||
@global document
|
||||
@global _CPRaiseInvalidAbstractInvocation
|
||||
|
||||
@protocol CPImageDelegate <CPObject>
|
||||
|
||||
@@ -139,6 +142,8 @@ CPImageNameColorPanelHighlighted = @"CPImageNameColorPanelHighlig
|
||||
var imagesForNames = { },
|
||||
ImageDescriptionFormat = "%s {\n filename: \"%s\",\n size: { width:%f, height:%f }\n}",
|
||||
CPImageSystemImageNameKey = @"CPImageSystemImageNameKey",
|
||||
CPImageAssetNameKey = @"CPImageAssetNameKey",
|
||||
CPImageAssetBundlePathKey = @"CPImageAssetBundlePathKey",
|
||||
|
||||
/*
|
||||
The system image names, each with its default image: the AppKit resource that holds the
|
||||
@@ -508,10 +513,11 @@ function _CPSystemImageValue(aName, anAppearance)
|
||||
|
||||
/*!
|
||||
Returns the image with the name \c aName: an image registered under that name with
|
||||
-setName:, or else the system image of that name, such as CPImageNameAddTemplate. A system
|
||||
image takes its picture from the current theme and the drawing appearance each time it is
|
||||
used, so it follows a theme switch. +imageNamed: returns one shared instance for each
|
||||
system image.
|
||||
-setName:, else the image of that name in the main bundle's asset catalog, else the system
|
||||
image of that name, such as CPImageNameAddTemplate. An asset image or a system image takes
|
||||
its picture from the asset catalog or the current theme, for the drawing appearance, each
|
||||
time it is used, so it follows the appearance and a theme switch. +imageNamed: returns one
|
||||
shared instance for each asset image and each system image.
|
||||
@param aName the name of the image
|
||||
@return the image, or \c nil when no image has that name
|
||||
*/
|
||||
@@ -519,6 +525,11 @@ function _CPSystemImageValue(aName, anAppearance)
|
||||
{
|
||||
var image = imagesForNames[aName];
|
||||
|
||||
if (image)
|
||||
return image;
|
||||
|
||||
image = [[_CPAssetCatalog catalogForBundle:[CPBundle mainBundle]] imageNamed:aName];
|
||||
|
||||
if (image)
|
||||
return image;
|
||||
|
||||
@@ -537,7 +548,7 @@ function _CPSystemImageValue(aName, anAppearance)
|
||||
var image = _CPSystemImages[aName];
|
||||
|
||||
if (!image)
|
||||
image = _CPSystemImages[aName] = [[_CPSystemImage alloc] _initWithSystemImageName:aName];
|
||||
image = _CPSystemImages[aName] = [[_CPSystemImage alloc] _initWithDynamicImageName:aName];
|
||||
|
||||
return image;
|
||||
}
|
||||
@@ -991,6 +1002,14 @@ var CPImageCSSDictionaryKey = @"CPImageCSSDictionaryKey",
|
||||
if ([aCoder containsValueForKey:CPImageSystemImageNameKey])
|
||||
return [CPImage _systemImageNamed:[aCoder decodeObjectForKey:CPImageSystemImageNameKey]];
|
||||
|
||||
if ([aCoder containsValueForKey:CPImageAssetNameKey])
|
||||
{
|
||||
var bundlePath = [aCoder decodeObjectForKey:CPImageAssetBundlePathKey],
|
||||
bundle = bundlePath ? [CPBundle bundleWithPath:bundlePath] : [CPBundle mainBundle];
|
||||
|
||||
return [[_CPAssetCatalog catalogForBundle:bundle] imageNamed:[aCoder decodeObjectForKey:CPImageAssetNameKey]];
|
||||
}
|
||||
|
||||
if ([aCoder containsValueForKey:CPImageCSSDictionaryKey])
|
||||
return [self initWithCSSDictionary:[aCoder decodeObjectForKey:CPImageCSSDictionaryKey] beforeDictionary:[aCoder decodeObjectForKey:CPImageCSSBeforeDictionaryKey] afterDictionary:[aCoder decodeObjectForKey:CPImageCSSAfterDictionaryKey] size:[aCoder decodeSizeForKey:@"CPSize"]];
|
||||
else
|
||||
@@ -1372,48 +1391,45 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
|
||||
// MARK: -
|
||||
|
||||
/*
|
||||
A system image keeps its name and resolves to an image each time it is used: the current
|
||||
theme's image for the drawing appearance, or the default image (see _CPSystemImageValue). So
|
||||
it follows a theme switch and the appearance in which a view draws. Each CPImage method
|
||||
forwards to the resolved image; the methods of CPImage subclasses forward through
|
||||
-forwardingTargetForSelector:. When there is no image, a system image is an empty, loaded
|
||||
image of size zero.
|
||||
A dynamic image holds no picture of its own and resolves to an image each time it is used
|
||||
(see -_resolvedImage), so that it follows a theme switch and the appearance in which a view
|
||||
draws. Each CPImage method forwards to the resolved image; the methods of CPImage
|
||||
subclasses forward through -forwardingTargetForSelector:. When there is no image, a dynamic
|
||||
image is an empty, loaded image of size zero. Subclasses implement -_resolvedImage and
|
||||
-encodeWithCoder:.
|
||||
*/
|
||||
@implementation _CPSystemImage : CPImage
|
||||
@implementation _CPDynamicImage : CPImage
|
||||
{
|
||||
CPString _systemImageName;
|
||||
CGSize _sizeOverride;
|
||||
CPImage _observedImage;
|
||||
}
|
||||
|
||||
- (id)_initWithSystemImageName:(CPString)aName
|
||||
- (id)_initWithDynamicImageName:(CPString)aName
|
||||
{
|
||||
self = [super initByReferencingFile:@"" size:CGSizeMakeZero()];
|
||||
|
||||
if (self)
|
||||
_systemImageName = aName;
|
||||
_name = aName;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the image that the receiver stands for in the current drawing appearance, or nil.
|
||||
*/
|
||||
- (CPImage)_resolvedImage
|
||||
{
|
||||
return _CPSystemImageValue(_systemImageName, [CPAppearance currentDrawingAppearance]);
|
||||
_CPRaiseInvalidAbstractInvocation(self, _cmd);
|
||||
}
|
||||
|
||||
// MARK: Name
|
||||
|
||||
- (CPString)name
|
||||
{
|
||||
return _systemImageName;
|
||||
}
|
||||
|
||||
/*
|
||||
The name of a system image is fixed.
|
||||
The name of a dynamic image is fixed.
|
||||
*/
|
||||
- (BOOL)setName:(CPString)aName
|
||||
{
|
||||
return aName === _systemImageName;
|
||||
return aName === _name;
|
||||
}
|
||||
|
||||
// MARK: Forwarding
|
||||
@@ -1558,10 +1574,6 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
|
||||
return [resolvedImage respondsToSelector:aSelector] ? resolvedImage : [super forwardingTargetForSelector:aSelector];
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return [CPString stringWithFormat:@"System image %@: %@", _systemImageName, [[self _resolvedImage] description]];
|
||||
}
|
||||
|
||||
// MARK: Loading
|
||||
|
||||
@@ -1600,12 +1612,110 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
|
||||
return [CPImage class];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
A system image is archived by its name, so that it decodes to the shared system image.
|
||||
A system image resolves to the current theme's image for the drawing appearance, or to the
|
||||
default image (see _CPSystemImageValue). It is archived by its name, so that it decodes to
|
||||
the shared system image.
|
||||
*/
|
||||
@implementation _CPSystemImage : _CPDynamicImage
|
||||
|
||||
- (CPImage)_resolvedImage
|
||||
{
|
||||
return _CPSystemImageValue(_name, [CPAppearance currentDrawingAppearance]);
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return [CPString stringWithFormat:@"System image %@: %@", _name, [[self _resolvedImage] description]];
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[aCoder encodeObject:_systemImageName forKey:CPImageSystemImageNameKey];
|
||||
[aCoder encodeObject:_name forKey:CPImageSystemImageNameKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
An image of a bundle's asset catalog (see +imageNamed:). It resolves to the catalog's image
|
||||
for the drawing appearance and the display scale. It is archived by its name and, outside
|
||||
the main bundle, its bundle path.
|
||||
*/
|
||||
@implementation _CPAssetImage : _CPDynamicImage
|
||||
{
|
||||
_CPAssetCatalog _catalog;
|
||||
}
|
||||
|
||||
- (id)_initWithAssetName:(CPString)aName catalog:(_CPAssetCatalog)aCatalog
|
||||
{
|
||||
self = [super _initWithDynamicImageName:aName];
|
||||
|
||||
if (self)
|
||||
_catalog = aCatalog;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (CPImage)_resolvedImage
|
||||
{
|
||||
return [_catalog _imageValueForAssetNamed:_name appearance:[CPAppearance currentDrawingAppearance]];
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return [CPString stringWithFormat:@"Asset image %@: %@", _name, [[self _resolvedImage] description]];
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
var bundle = [_catalog bundle];
|
||||
|
||||
[aCoder encodeObject:_name forKey:CPImageAssetNameKey];
|
||||
|
||||
if (bundle !== [CPBundle mainBundle])
|
||||
[aCoder encodeObject:[bundle bundlePath] forKey:CPImageAssetBundlePathKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
An image file of an asset catalog. Its size is its pixel size divided by its scale, as an
|
||||
image of scale 2x covers the area of half its pixel size.
|
||||
*/
|
||||
@implementation _CPScaledImage : CPImage
|
||||
{
|
||||
float _scale;
|
||||
}
|
||||
|
||||
- (id)_initWithContentsOfFile:(CPString)aFilename scale:(float)aScale
|
||||
{
|
||||
self = [self initByReferencingFile:aFilename size:CGSizeMake(-1, -1)];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_scale = aScale;
|
||||
[self load];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (float)_scale
|
||||
{
|
||||
return _scale;
|
||||
}
|
||||
|
||||
- (void)_imageDidLoad
|
||||
{
|
||||
if (_scale !== 1.0 && (!_size || (_size.width == -1 && _size.height == -1)))
|
||||
_size = CGSizeMake(_image.width / _scale, _image.height / _scale);
|
||||
|
||||
[super _imageDidLoad];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,531 @@
|
||||
/*
|
||||
* _CPAssetCatalog.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-09.
|
||||
* Copyright David Richardson 2026.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <Foundation/CPBundle.j>
|
||||
@import <Foundation/CPObject.j>
|
||||
@import <Foundation/CPURL.j>
|
||||
|
||||
@import "CPAppearance.j"
|
||||
|
||||
/*
|
||||
CPColor.j and CPImage.j import this file, so it refers to their classes by name only and
|
||||
imports neither.
|
||||
*/
|
||||
@class CPColor
|
||||
@class CPImage
|
||||
@class _CPAssetColor
|
||||
@class _CPAssetImage
|
||||
@class _CPScaledImage
|
||||
|
||||
@global window
|
||||
|
||||
/*
|
||||
The asset catalog of a bundle. The build compiles the bundle's .xcassets directories into
|
||||
one table, a JSON resource that the Info.plist key CPAssetCatalog names. The table keeps
|
||||
the vocabulary of Apple's asset catalogs:
|
||||
|
||||
{
|
||||
"version": 1,
|
||||
"colors": { "<name>": [ <colour entry>, ... ] },
|
||||
"images": { "<name>": { "properties": { ... }, "images": [ <image entry>, ... ] } }
|
||||
}
|
||||
|
||||
A colour entry and an image entry are entries of the asset's Contents.json. An image
|
||||
entry's "filename" is the path of the image resource, relative to the bundle's Resources
|
||||
directory.
|
||||
*/
|
||||
|
||||
var _CPAssetCatalogInfoKey = @"CPAssetCatalog",
|
||||
_CPAssetCatalogs = {},
|
||||
|
||||
/*
|
||||
The components of a Display P3 colour, linear, convert to linear sRGB by this matrix.
|
||||
Both spaces use the D65 white point and the sRGB transfer function.
|
||||
*/
|
||||
_CPDisplayP3ToSRGBMatrix = [
|
||||
[ 1.2249401, -0.2249404, 0.0000000],
|
||||
[-0.0420569, 1.0420571, 0.0000000],
|
||||
[-0.0196376, -0.0786361, 1.0982735]
|
||||
];
|
||||
|
||||
/// @cond IGNORE
|
||||
|
||||
/*
|
||||
Returns the luminosity ("light" or "dark") and contrast ("normal" or "high") of the
|
||||
appearance named aName. A vibrant appearance and light content take the luminosity of their
|
||||
lightness.
|
||||
*/
|
||||
function _CPAssetTraitsForAppearanceName(aName)
|
||||
{
|
||||
switch (aName)
|
||||
{
|
||||
case CPAppearanceNameDarkAqua:
|
||||
case CPAppearanceNameVibrantDark:
|
||||
return { luminosity: "dark", contrast: "normal" };
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastAqua:
|
||||
case CPAppearanceNameAccessibilityHighContrastVibrantLight:
|
||||
return { luminosity: "light", contrast: "high" };
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastDarkAqua:
|
||||
case CPAppearanceNameAccessibilityHighContrastVibrantDark:
|
||||
return { luminosity: "dark", contrast: "high" };
|
||||
|
||||
default:
|
||||
return { luminosity: "light", contrast: "normal" };
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the luminosity ("light", "dark" or "any") and contrast ("normal" or "high") that
|
||||
the "appearances" of anEntry name. An entry without luminosity applies to any luminosity,
|
||||
and an entry without contrast applies to normal contrast.
|
||||
*/
|
||||
function _CPAssetTraitsOfEntry(anEntry)
|
||||
{
|
||||
var traits = { luminosity: "any", contrast: "normal" },
|
||||
appearances = anEntry.appearances || [];
|
||||
|
||||
for (var i = 0, count = appearances.length; i < count; i++)
|
||||
{
|
||||
var appearance = appearances[i];
|
||||
|
||||
if (appearance.appearance === "luminosity")
|
||||
traits.luminosity = appearance.value;
|
||||
else if (appearance.appearance === "contrast")
|
||||
traits.contrast = appearance.value;
|
||||
}
|
||||
|
||||
return traits;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the entries of someEntries that best suit the appearance named aName, or an empty
|
||||
array. The order of preference: the exact luminosity and contrast, then the same luminosity
|
||||
at normal contrast, then any luminosity at the same contrast, then any luminosity at normal
|
||||
contrast.
|
||||
*/
|
||||
function _CPAssetEntriesForAppearanceName(someEntries, aName)
|
||||
{
|
||||
var wanted = _CPAssetTraitsForAppearanceName(aName),
|
||||
candidates = [[wanted.luminosity, wanted.contrast]];
|
||||
|
||||
if (wanted.contrast !== "normal")
|
||||
candidates.push([wanted.luminosity, "normal"]);
|
||||
|
||||
candidates.push(["any", wanted.contrast]);
|
||||
|
||||
if (wanted.contrast !== "normal")
|
||||
candidates.push(["any", "normal"]);
|
||||
|
||||
for (var i = 0, count = candidates.length; i < count; i++)
|
||||
{
|
||||
var matches = [];
|
||||
|
||||
for (var j = 0, entryCount = someEntries.length; j < entryCount; j++)
|
||||
{
|
||||
var entry = someEntries[j],
|
||||
traits = _CPAssetTraitsOfEntry(entry);
|
||||
|
||||
if (traits.luminosity === candidates[i][0] && traits.contrast === candidates[i][1])
|
||||
matches.push(entry);
|
||||
}
|
||||
|
||||
if (matches.length)
|
||||
return matches;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the entries of someEntries for the most specific idiom: "mac" before "universal".
|
||||
*/
|
||||
function _CPAssetEntriesForIdiom(someEntries)
|
||||
{
|
||||
var macEntries = someEntries.filter(function(anEntry) { return anEntry.idiom === "mac"; });
|
||||
|
||||
return macEntries.length ? macEntries : someEntries;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the entries of someEntries for the display gamut: entries for wantedGamut
|
||||
("sRGB" or "display-P3") or for any gamut, preferring the first.
|
||||
*/
|
||||
function _CPAssetEntriesForGamut(someEntries, wantedGamut)
|
||||
{
|
||||
var gamutEntries = someEntries.filter(function(anEntry) { return anEntry["display-gamut"] === wantedGamut; });
|
||||
|
||||
if (gamutEntries.length)
|
||||
return gamutEntries;
|
||||
|
||||
var anyGamutEntries = someEntries.filter(function(anEntry) { return !anEntry["display-gamut"]; });
|
||||
|
||||
return anyGamutEntries.length ? anyGamutEntries : someEntries;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the value of a colour component as Xcode writes it: floating point ("0.502"),
|
||||
8-bit hexadecimal ("0x80") or an 8-bit integer ("128"). An alpha component without a
|
||||
decimal point is an 8-bit integer only when it exceeds 1.
|
||||
*/
|
||||
function _CPAssetComponentValue(aComponent, isAlpha)
|
||||
{
|
||||
if (aComponent === undefined || aComponent === null)
|
||||
return isAlpha ? 1.0 : 0.0;
|
||||
|
||||
if (typeof aComponent === "number")
|
||||
return aComponent;
|
||||
|
||||
var string = String(aComponent).trim();
|
||||
|
||||
if (/^[-+]?0x/i.test(string))
|
||||
return parseInt(string, 16) / 255.0;
|
||||
|
||||
var value = parseFloat(string);
|
||||
|
||||
if (string.indexOf(".") === -1 && (!isAlpha || value > 1))
|
||||
return value / 255.0;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function _CPSRGBToLinear(aValue)
|
||||
{
|
||||
var magnitude = Math.abs(aValue),
|
||||
linear = magnitude <= 0.04045 ? magnitude / 12.92 : Math.pow((magnitude + 0.055) / 1.055, 2.4);
|
||||
|
||||
return aValue < 0 ? -linear : linear;
|
||||
}
|
||||
|
||||
function _CPLinearToSRGB(aValue)
|
||||
{
|
||||
var magnitude = Math.abs(aValue),
|
||||
encoded = magnitude <= 0.0031308 ? magnitude * 12.92 : 1.055 * Math.pow(magnitude, 1 / 2.4) - 0.055;
|
||||
|
||||
return aValue < 0 ? -encoded : encoded;
|
||||
}
|
||||
|
||||
function _CPClampComponent(aValue)
|
||||
{
|
||||
return MIN(1.0, MAX(0.0, aValue));
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the CPColor for the "color" object of a colour entry, or nil when the entry names
|
||||
an unknown colour space or an unknown System catalog colour.
|
||||
*/
|
||||
function _CPAssetColorFromDescription(aDescription)
|
||||
{
|
||||
if (aDescription.reference)
|
||||
{
|
||||
var referencedColor = [CPColor colorWithCatalogName:@"System" colorName:aDescription.reference];
|
||||
|
||||
if (!referencedColor)
|
||||
CPLog.warn("Asset catalog: unknown System colour \"" + aDescription.reference + "\"");
|
||||
|
||||
return referencedColor;
|
||||
}
|
||||
|
||||
var components = aDescription.components || {},
|
||||
colorSpace = aDescription["color-space"] || "srgb",
|
||||
alpha = _CPClampComponent(_CPAssetComponentValue(components.alpha, YES));
|
||||
|
||||
switch (colorSpace)
|
||||
{
|
||||
case "srgb":
|
||||
case "extended-srgb":
|
||||
return [CPColor colorWithSRGBRed:_CPClampComponent(_CPAssetComponentValue(components.red, NO))
|
||||
green:_CPClampComponent(_CPAssetComponentValue(components.green, NO))
|
||||
blue:_CPClampComponent(_CPAssetComponentValue(components.blue, NO))
|
||||
alpha:alpha];
|
||||
|
||||
case "extended-linear-srgb":
|
||||
return [CPColor colorWithSRGBRed:_CPClampComponent(_CPLinearToSRGB(_CPAssetComponentValue(components.red, NO)))
|
||||
green:_CPClampComponent(_CPLinearToSRGB(_CPAssetComponentValue(components.green, NO)))
|
||||
blue:_CPClampComponent(_CPLinearToSRGB(_CPAssetComponentValue(components.blue, NO)))
|
||||
alpha:alpha];
|
||||
|
||||
case "display-p3":
|
||||
var linear = [_CPSRGBToLinear(_CPAssetComponentValue(components.red, NO)),
|
||||
_CPSRGBToLinear(_CPAssetComponentValue(components.green, NO)),
|
||||
_CPSRGBToLinear(_CPAssetComponentValue(components.blue, NO))],
|
||||
srgb = _CPDisplayP3ToSRGBMatrix.map(function(aRow)
|
||||
{
|
||||
return _CPClampComponent(_CPLinearToSRGB(aRow[0] * linear[0] + aRow[1] * linear[1] + aRow[2] * linear[2]));
|
||||
});
|
||||
|
||||
return [CPColor colorWithSRGBRed:srgb[0] green:srgb[1] blue:srgb[2] alpha:alpha];
|
||||
|
||||
case "gray-gamma-22":
|
||||
case "extended-gray":
|
||||
/*
|
||||
Gray components carry gamma 2.2; the sRGB value encodes the same linear
|
||||
luminance.
|
||||
*/
|
||||
var white = _CPAssetComponentValue(components.white, NO),
|
||||
linearWhite = (white < 0 ? -1 : 1) * Math.pow(Math.abs(white), 2.2),
|
||||
srgbWhite = _CPClampComponent(_CPLinearToSRGB(linearWhite));
|
||||
|
||||
return [CPColor colorWithSRGBRed:srgbWhite green:srgbWhite blue:srgbWhite alpha:alpha];
|
||||
|
||||
default:
|
||||
CPLog.warn("Asset catalog: unsupported colour space \"" + colorSpace + "\"");
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the scale of an image entry ("2x" gives 2), or nil for a single-scale entry.
|
||||
*/
|
||||
function _CPAssetScaleOfEntry(anEntry)
|
||||
{
|
||||
return anEntry.scale ? parseFloat(anEntry.scale) : nil;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the entry of someEntries for aDisplayScale: a single-scale entry when there is one,
|
||||
else the entry of the smallest scale not below aDisplayScale, else the entry of the largest
|
||||
scale.
|
||||
*/
|
||||
function _CPAssetEntryForDisplayScale(someEntries, aDisplayScale)
|
||||
{
|
||||
var bestAbove = nil,
|
||||
largest = nil;
|
||||
|
||||
for (var i = 0, count = someEntries.length; i < count; i++)
|
||||
{
|
||||
var entry = someEntries[i],
|
||||
scale = _CPAssetScaleOfEntry(entry);
|
||||
|
||||
if (scale === nil)
|
||||
return entry;
|
||||
|
||||
if (scale >= aDisplayScale && (!bestAbove || scale < _CPAssetScaleOfEntry(bestAbove)))
|
||||
bestAbove = entry;
|
||||
|
||||
if (!largest || scale > _CPAssetScaleOfEntry(largest))
|
||||
largest = entry;
|
||||
}
|
||||
|
||||
return bestAbove || largest;
|
||||
}
|
||||
|
||||
function _CPAssetDisplayScale()
|
||||
{
|
||||
return (typeof window !== "undefined" && window.devicePixelRatio) || 1;
|
||||
}
|
||||
|
||||
function _CPAssetDisplayGamut()
|
||||
{
|
||||
if (typeof window !== "undefined" && window.matchMedia && window.matchMedia("(color-gamut: p3)").matches)
|
||||
return "display-P3";
|
||||
|
||||
return "sRGB";
|
||||
}
|
||||
|
||||
/// @endcond
|
||||
|
||||
|
||||
@implementation _CPAssetCatalog : CPObject
|
||||
{
|
||||
CPBundle _bundle @accessors(getter=bundle);
|
||||
JSObject _colorEntries;
|
||||
JSObject _imageAssets;
|
||||
JSObject _colors;
|
||||
JSObject _images;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the asset catalog of aBundle (the main bundle when aBundle is nil), or nil when the
|
||||
bundle has none.
|
||||
*/
|
||||
+ (_CPAssetCatalog)catalogForBundle:(CPBundle)aBundle
|
||||
{
|
||||
var bundle = aBundle || [CPBundle mainBundle],
|
||||
key = [bundle bundlePath];
|
||||
|
||||
if (_CPAssetCatalogs.hasOwnProperty(key))
|
||||
return _CPAssetCatalogs[key];
|
||||
|
||||
var catalog = [self _catalogFromBundle:bundle];
|
||||
|
||||
/*
|
||||
A bundle that has not finished loading may still gain its Info.plist, so only a loaded
|
||||
bundle without a catalog is remembered as having none.
|
||||
*/
|
||||
if (catalog || [bundle isLoaded])
|
||||
_CPAssetCatalogs[key] = catalog;
|
||||
|
||||
return catalog;
|
||||
}
|
||||
|
||||
/*
|
||||
Makes aCatalog the asset catalog of aBundle; a nil catalog makes the bundle load its own
|
||||
catalog again on next use.
|
||||
*/
|
||||
+ (void)_setCatalog:(_CPAssetCatalog)aCatalog forBundle:(CPBundle)aBundle
|
||||
{
|
||||
var key = [(aBundle || [CPBundle mainBundle]) bundlePath];
|
||||
|
||||
if (aCatalog)
|
||||
_CPAssetCatalogs[key] = aCatalog;
|
||||
else
|
||||
delete _CPAssetCatalogs[key];
|
||||
}
|
||||
|
||||
+ (_CPAssetCatalog)_catalogFromBundle:(CPBundle)aBundle
|
||||
{
|
||||
var resourceName = [aBundle objectForInfoDictionaryKey:_CPAssetCatalogInfoKey];
|
||||
|
||||
if (!resourceName)
|
||||
return nil;
|
||||
|
||||
var data = [[CPURL URLWithString:[aBundle pathForResource:resourceName]] staticResourceData];
|
||||
|
||||
if (!data)
|
||||
{
|
||||
CPLog.warn("Asset catalog: bundle " + [aBundle bundlePath] + " names \"" + resourceName + "\", which is not in its static file");
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [[self alloc] _initWithContents:JSON.parse([data rawString]) bundle:aBundle];
|
||||
}
|
||||
|
||||
/*
|
||||
Initializes the catalog of aBundle with the compiled table someContents.
|
||||
*/
|
||||
- (id)_initWithContents:(JSObject)someContents bundle:(CPBundle)aBundle
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_bundle = aBundle;
|
||||
_colorEntries = someContents.colors || {};
|
||||
_imageAssets = someContents.images || {};
|
||||
_colors = {};
|
||||
_images = {};
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
// MARK: Colours
|
||||
|
||||
/*
|
||||
Returns the shared asset colour named aName, or nil when the catalog has no colour of that
|
||||
name.
|
||||
*/
|
||||
- (CPColor)colorNamed:(CPString)aName
|
||||
{
|
||||
if (!_colorEntries.hasOwnProperty(aName))
|
||||
return nil;
|
||||
|
||||
var color = _colors[aName];
|
||||
|
||||
if (!color)
|
||||
color = _colors[aName] = [[_CPAssetColor alloc] _initWithAssetName:aName catalog:self];
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the value of the colour named aName in anAppearance, or nil when no entry suits the
|
||||
appearance. The catalog keeps each entry's colour once made.
|
||||
*/
|
||||
- (CPColor)_colorValueForAssetNamed:(CPString)aName appearance:(CPAppearance)anAppearance
|
||||
{
|
||||
var entries = _CPAssetEntriesForAppearanceName(_colorEntries[aName] || [], [anAppearance name]);
|
||||
|
||||
/*
|
||||
CPColor holds sRGB values only, so an entry for the sRGB gamut serves better than an
|
||||
entry for Display P3 converted to sRGB.
|
||||
*/
|
||||
entries = _CPAssetEntriesForGamut(_CPAssetEntriesForIdiom(entries), "sRGB");
|
||||
|
||||
var entry = entries[0];
|
||||
|
||||
if (!entry)
|
||||
return nil;
|
||||
|
||||
if (!entry.hasOwnProperty("_CPColorValue"))
|
||||
entry._CPColorValue = entry.color ? _CPAssetColorFromDescription(entry.color) : nil;
|
||||
|
||||
return entry._CPColorValue;
|
||||
}
|
||||
|
||||
// MARK: Images
|
||||
|
||||
/*
|
||||
Returns the shared asset image named aName, or nil when the catalog has no image of that
|
||||
name.
|
||||
*/
|
||||
- (CPImage)imageNamed:(CPString)aName
|
||||
{
|
||||
if (!_imageAssets.hasOwnProperty(aName))
|
||||
return nil;
|
||||
|
||||
var image = _images[aName];
|
||||
|
||||
if (!image)
|
||||
image = _images[aName] = [[_CPAssetImage alloc] _initWithAssetName:aName catalog:self];
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the properties of the image named aName, such as "template-rendering-intent".
|
||||
*/
|
||||
- (JSObject)_propertiesOfImageNamed:(CPString)aName
|
||||
{
|
||||
var asset = _imageAssets[aName];
|
||||
|
||||
return (asset && asset.properties) || {};
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the image named aName for anAppearance and the display scale, or nil when no entry
|
||||
suits the appearance. The catalog keeps each entry's image once made.
|
||||
*/
|
||||
- (CPImage)_imageValueForAssetNamed:(CPString)aName appearance:(CPAppearance)anAppearance
|
||||
{
|
||||
var asset = _imageAssets[aName],
|
||||
entries = ((asset && asset.images) || []).filter(function(anEntry) { return !!anEntry.filename; });
|
||||
|
||||
entries = _CPAssetEntriesForAppearanceName(entries, [anAppearance name]);
|
||||
entries = _CPAssetEntriesForGamut(_CPAssetEntriesForIdiom(entries), _CPAssetDisplayGamut());
|
||||
|
||||
var entry = _CPAssetEntryForDisplayScale(entries, _CPAssetDisplayScale());
|
||||
|
||||
if (!entry)
|
||||
return nil;
|
||||
|
||||
if (!entry._CPImageValue)
|
||||
entry._CPImageValue = [[_CPScaledImage alloc] _initWithContentsOfFile:[_bundle pathForResource:entry.filename]
|
||||
scale:_CPAssetScaleOfEntry(entry) || 1.0];
|
||||
|
||||
return entry._CPImageValue;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* CPAssetCatalogTest.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-09.
|
||||
* Copyright David Richardson 2026.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <AppKit/CPAppearance.j>
|
||||
@import <AppKit/CPColor.j>
|
||||
@import <AppKit/CPImage.j>
|
||||
@import <AppKit/_CPAssetCatalog.j>
|
||||
|
||||
var CPAssetCatalogTestDark = [{ appearance: "luminosity", value: "dark" }],
|
||||
CPAssetCatalogTestHighContrast = [{ appearance: "contrast", value: "high" }];
|
||||
|
||||
function CPAssetCatalogTestSRGB(red, green, blue, alpha)
|
||||
{
|
||||
return { "color-space": "srgb", components: { red: red, green: green, blue: blue, alpha: alpha } };
|
||||
}
|
||||
|
||||
/*
|
||||
A compiled asset catalog table, as the build writes it.
|
||||
*/
|
||||
function CPAssetCatalogTestContents()
|
||||
{
|
||||
return {
|
||||
version: 1,
|
||||
colors: {
|
||||
"Background": [
|
||||
{ idiom: "universal", color: CPAssetCatalogTestSRGB("1.000", "1.000", "1.000", "1.000") },
|
||||
{ idiom: "universal", appearances: CPAssetCatalogTestDark, color: CPAssetCatalogTestSRGB("0.000", "0.000", "0.000", "1.000") },
|
||||
{ idiom: "universal", appearances: CPAssetCatalogTestHighContrast, color: CPAssetCatalogTestSRGB("0.500", "0.500", "0.500", "1.000") }
|
||||
],
|
||||
"FloatingPoint": [{ idiom: "universal", color: CPAssetCatalogTestSRGB("0.502", "0.000", "0.000", "0.500") }],
|
||||
"EightBit": [{ idiom: "universal", color: CPAssetCatalogTestSRGB("128", "0", "255", "1.000") }],
|
||||
"Hexadecimal": [{ idiom: "universal", color: CPAssetCatalogTestSRGB("0x80", "0x00", "0xFF", "1.000") }],
|
||||
"Gray": [{ idiom: "universal", color: { "color-space": "gray-gamma-22", components: { white: "0.500", alpha: "1.000" } } }],
|
||||
"LinearGray": [{ idiom: "universal", color: { "color-space": "extended-linear-srgb", components: { red: "0.214041", green: "0.214041", blue: "0.214041", alpha: "1.000" } } }],
|
||||
"DisplayP3Gray": [{ idiom: "universal", color: { "color-space": "display-p3", components: { red: "0.500", green: "0.500", blue: "0.500", alpha: "1.000" } } }],
|
||||
"DisplayP3Green": [{ idiom: "universal", color: { "color-space": "display-p3", components: { red: "0.000", green: "0.500", blue: "0.000", alpha: "1.000" } } }],
|
||||
"Accent": [{ idiom: "universal", color: { platform: "osx", reference: "systemBlueColor" } }],
|
||||
"MacIdiom": [
|
||||
{ idiom: "universal", color: CPAssetCatalogTestSRGB("1.000", "0.000", "0.000", "1.000") },
|
||||
{ idiom: "mac", color: CPAssetCatalogTestSRGB("0.000", "1.000", "0.000", "1.000") }
|
||||
]
|
||||
},
|
||||
images: {
|
||||
"Logo": {
|
||||
properties: { "template-rendering-intent": "template" },
|
||||
images: [
|
||||
{ idiom: "universal", filename: "Assets.xcassets/Logo.imageset/logo.png", scale: "1x" },
|
||||
{ idiom: "universal", filename: "Assets.xcassets/Logo.imageset/logo@2x.png", scale: "2x" },
|
||||
{ idiom: "universal", scale: "3x" },
|
||||
{ idiom: "universal", appearances: CPAssetCatalogTestDark, filename: "Assets.xcassets/Logo.imageset/logo-dark.png", scale: "1x" }
|
||||
]
|
||||
},
|
||||
"CPAddTemplate": {
|
||||
images: [{ idiom: "universal", filename: "Assets.xcassets/Add.imageset/add.png" }]
|
||||
},
|
||||
"CPAssetCatalogTestRegistered": {
|
||||
images: [{ idiom: "universal", filename: "Assets.xcassets/Registered.imageset/registered.png" }]
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@implementation CPAssetCatalogTest : OJTestCase
|
||||
|
||||
/*
|
||||
Each test gives the main bundle a fresh catalog, so that no test sees values that another
|
||||
test resolved.
|
||||
*/
|
||||
- (void)setUp
|
||||
{
|
||||
var mainBundle = [CPBundle mainBundle];
|
||||
|
||||
[_CPAssetCatalog _setCatalog:[[_CPAssetCatalog alloc] _initWithContents:CPAssetCatalogTestContents() bundle:mainBundle] forBundle:mainBundle];
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
[_CPAssetCatalog _setCatalog:nil forBundle:[CPBundle mainBundle]];
|
||||
[CPAppearance setCurrentAppearance:nil];
|
||||
}
|
||||
|
||||
- (void)setDrawingAppearanceNamed:(CPString)aName
|
||||
{
|
||||
[CPAppearance setCurrentAppearance:[CPAppearance appearanceNamed:aName]];
|
||||
}
|
||||
|
||||
- (void)assertColor:(CPColor)aColor red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha message:(CPString)aMessage
|
||||
{
|
||||
var actual = [[aColor redComponent], [aColor greenComponent], [aColor blueComponent], [aColor alphaComponent]],
|
||||
expected = [red, green, blue, alpha];
|
||||
|
||||
for (var i = 0; i < 4; i++)
|
||||
[self assertTrue:Math.abs(actual[i] - expected[i]) < 0.001 message:aMessage + ": expected " + expected + " but was " + actual];
|
||||
}
|
||||
|
||||
// MARK: Colours
|
||||
|
||||
- (void)testColorNamedGivesTheSharedAssetColor
|
||||
{
|
||||
var color = [CPColor colorNamed:@"Background"];
|
||||
|
||||
[self assertNotNull:color];
|
||||
[self assert:color same:[CPColor colorNamed:@"Background"]];
|
||||
[self assert:color same:[CPColor colorNamed:@"Background" bundle:[CPBundle mainBundle]]];
|
||||
}
|
||||
|
||||
- (void)testAnUnknownColorNameGivesNil
|
||||
{
|
||||
[self assertNull:[CPColor colorNamed:@"NoSuchColor"]];
|
||||
}
|
||||
|
||||
- (void)testABundleWithoutCatalogGivesNoColor
|
||||
{
|
||||
[self assertNull:[CPColor colorNamed:@"Background" bundle:[CPBundle bundleWithPath:@"CPAssetCatalogTestMissing.bundle"]]];
|
||||
}
|
||||
|
||||
- (void)testAnAssetColorTakesTheEntryForTheDrawingAppearance
|
||||
{
|
||||
var color = [CPColor colorNamed:@"Background"];
|
||||
|
||||
[self assertColor:color red:1.0 green:1.0 blue:1.0 alpha:1.0 message:@"Aqua"];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameDarkAqua];
|
||||
[self assertColor:color red:0.0 green:0.0 blue:0.0 alpha:1.0 message:@"DarkAqua"];
|
||||
}
|
||||
|
||||
- (void)testVibrantAppearancesTakeTheLuminosityOfTheirLightness
|
||||
{
|
||||
var color = [CPColor colorNamed:@"Background"];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameVibrantDark];
|
||||
[self assertColor:color red:0.0 green:0.0 blue:0.0 alpha:1.0 message:@"VibrantDark"];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameVibrantLight];
|
||||
[self assertColor:color red:1.0 green:1.0 blue:1.0 alpha:1.0 message:@"VibrantLight"];
|
||||
}
|
||||
|
||||
/*
|
||||
High contrast looks for the same luminosity at normal contrast before any luminosity at
|
||||
high contrast.
|
||||
*/
|
||||
- (void)testHighContrastPrefersTheSameLuminosityBeforeAnyLuminosity
|
||||
{
|
||||
var color = [CPColor colorNamed:@"Background"];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameAccessibilityHighContrastDarkAqua];
|
||||
[self assertColor:color red:0.0 green:0.0 blue:0.0 alpha:1.0 message:@"high-contrast DarkAqua"];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameAccessibilityHighContrastAqua];
|
||||
[self assertColor:color red:0.5 green:0.5 blue:0.5 alpha:1.0 message:@"high-contrast Aqua"];
|
||||
}
|
||||
|
||||
- (void)testComponentsReadEveryXcodeNotation
|
||||
{
|
||||
[self assertColor:[CPColor colorNamed:@"FloatingPoint"] red:0.502 green:0.0 blue:0.0 alpha:0.5 message:@"floating point"];
|
||||
[self assertColor:[CPColor colorNamed:@"EightBit"] red:128 / 255 green:0.0 blue:1.0 alpha:1.0 message:@"8-bit integer"];
|
||||
[self assertColor:[CPColor colorNamed:@"Hexadecimal"] red:128 / 255 green:0.0 blue:1.0 alpha:1.0 message:@"8-bit hexadecimal"];
|
||||
}
|
||||
|
||||
- (void)testGrayLinearAndDisplayP3ComponentsConvertToSRGB
|
||||
{
|
||||
[self assertColor:[CPColor colorNamed:@"Gray"] red:0.5039 green:0.5039 blue:0.5039 alpha:1.0 message:@"gray gamma 2.2"];
|
||||
[self assertColor:[CPColor colorNamed:@"LinearGray"] red:0.5 green:0.5 blue:0.5 alpha:1.0 message:@"linear sRGB"];
|
||||
[self assertColor:[CPColor colorNamed:@"DisplayP3Gray"] red:0.5 green:0.5 blue:0.5 alpha:1.0 message:@"Display P3 gray"];
|
||||
[self assertColor:[CPColor colorNamed:@"DisplayP3Green"] red:0.0 green:0.5096 blue:0.0 alpha:1.0 message:@"Display P3 green"];
|
||||
}
|
||||
|
||||
- (void)testAReferenceEntryGivesTheSystemColor
|
||||
{
|
||||
[self assert:[CPColor systemBlueColor] same:[[CPColor colorNamed:@"Accent"] _resolvedColor]];
|
||||
}
|
||||
|
||||
- (void)testTheMacIdiomComesBeforeUniversal
|
||||
{
|
||||
[self assertColor:[CPColor colorNamed:@"MacIdiom"] red:0.0 green:1.0 blue:0.0 alpha:1.0 message:@"mac idiom"];
|
||||
}
|
||||
|
||||
- (void)testAnAssetColorWithAlphaStaysAnAssetColor
|
||||
{
|
||||
var color = [[CPColor colorNamed:@"Background"] colorWithAlphaComponent:0.25];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameDarkAqua];
|
||||
[self assertColor:color red:0.0 green:0.0 blue:0.0 alpha:0.25 message:@"DarkAqua with alpha"];
|
||||
[self assert:color equals:[[CPColor colorNamed:@"Background"] colorWithAlphaComponent:0.25]];
|
||||
[self assert:color notEqual:[CPColor colorNamed:@"Background"]];
|
||||
}
|
||||
|
||||
- (void)testAnAssetColorIsArchivedByName
|
||||
{
|
||||
var color = [CPColor colorNamed:@"Background"],
|
||||
withAlpha = [color colorWithAlphaComponent:0.25];
|
||||
|
||||
[self assert:color same:[CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:color]]];
|
||||
[self assert:withAlpha equals:[CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:withAlpha]]];
|
||||
}
|
||||
|
||||
// MARK: Images
|
||||
|
||||
- (void)testImageNamedFindsTheSharedAssetImage
|
||||
{
|
||||
var image = [CPImage imageNamed:@"Logo"];
|
||||
|
||||
[self assertTrue:[image isKindOfClass:[_CPAssetImage class]]];
|
||||
[self assert:@"Logo" equals:[image name]];
|
||||
[self assert:image same:[CPImage imageNamed:@"Logo"]];
|
||||
}
|
||||
|
||||
- (void)testAnAssetComesBeforeASystemImageOfTheSameName
|
||||
{
|
||||
[self assertTrue:[[CPImage imageNamed:CPImageNameAddTemplate] isKindOfClass:[_CPAssetImage class]]];
|
||||
}
|
||||
|
||||
- (void)testARegisteredImageComesBeforeAnAsset
|
||||
{
|
||||
var registered = [[CPImage alloc] initByReferencingFile:@"registered.png" size:CGSizeMake(4, 4)];
|
||||
|
||||
[registered setName:@"CPAssetCatalogTestRegistered"];
|
||||
|
||||
[self assert:registered same:[CPImage imageNamed:@"CPAssetCatalogTestRegistered"]];
|
||||
}
|
||||
|
||||
- (void)testAnAssetImageTakesTheEntryForTheDrawingAppearance
|
||||
{
|
||||
var image = [CPImage imageNamed:@"Logo"],
|
||||
mainBundle = [CPBundle mainBundle];
|
||||
|
||||
[self assert:[mainBundle pathForResource:@"Assets.xcassets/Logo.imageset/logo.png"] equals:[[image _resolvedImage] filename]];
|
||||
|
||||
[self setDrawingAppearanceNamed:CPAppearanceNameDarkAqua];
|
||||
[self assert:[mainBundle pathForResource:@"Assets.xcassets/Logo.imageset/logo-dark.png"] equals:[[image _resolvedImage] filename]];
|
||||
}
|
||||
|
||||
- (void)testTheDisplayScaleChoosesTheImageEntry
|
||||
{
|
||||
var entries = [{ scale: "1x", filename: "a" }, { scale: "2x", filename: "b" }, { scale: "3x", filename: "c" }],
|
||||
withSingleScale = [{ scale: "2x", filename: "b" }, { filename: "vector" }];
|
||||
|
||||
[self assert:entries[0] same:_CPAssetEntryForDisplayScale(entries, 1)];
|
||||
[self assert:entries[1] same:_CPAssetEntryForDisplayScale(entries, 1.5)];
|
||||
[self assert:entries[1] same:_CPAssetEntryForDisplayScale(entries, 2)];
|
||||
[self assert:entries[2] same:_CPAssetEntryForDisplayScale(entries, 4)];
|
||||
[self assert:withSingleScale[1] same:_CPAssetEntryForDisplayScale(withSingleScale, 2)];
|
||||
}
|
||||
|
||||
- (void)testAnImageOfScale2xCoversHalfItsPixelSize
|
||||
{
|
||||
var image = [[_CPScaledImage alloc] _initWithContentsOfFile:@"logo@2x.png" scale:2.0];
|
||||
|
||||
image._image = { width: 40, height: 20 };
|
||||
[image _imageDidLoad];
|
||||
|
||||
[self assertTrue:CGSizeEqualToSize(CGSizeMake(20, 10), [image size])];
|
||||
}
|
||||
|
||||
- (void)testTheTemplateRenderingIntentStaysInTheCatalog
|
||||
{
|
||||
var catalog = [_CPAssetCatalog catalogForBundle:nil];
|
||||
|
||||
[self assert:@"template" equals:[catalog _propertiesOfImageNamed:@"Logo"]["template-rendering-intent"]];
|
||||
}
|
||||
|
||||
- (void)testAnAssetImageIsArchivedByName
|
||||
{
|
||||
var image = [CPImage imageNamed:@"Logo"];
|
||||
|
||||
[self assert:image same:[CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:image]]];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user