Skip to content

Commit

Permalink
ColorSpace support
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed May 21, 2024
1 parent 388f93e commit 2e1e22e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/SharpMetal.Examples.Common/MTKView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public MTLPixelFormat ColorPixelFormat
set => ObjectiveC.objc_msgSend(NativePtr, "setColorPixelFormat:atIndex:", (ulong)value, 0);
}

public IntPtr Colorspace
{
get => ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, new Selector("colorspace"));
set => ObjectiveCRuntime.objc_msgSend(NativePtr, new Selector("setColorspace:"), value);
}

public MTLClearColor ClearColor
{
set => ObjectiveCRuntime.objc_msgSend(NativePtr, new Selector("setClearColor:"), value);
Expand Down
1 change: 1 addition & 0 deletions src/SharpMetal.Examples.Primitive/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SharpMetal.Foundation;
using SharpMetal.Examples.Common;
using SharpMetal.ObjectiveCCore;
using SharpMetal.QuartzCore;

namespace SharpMetal.Examples.Primitive
{
Expand Down
15 changes: 14 additions & 1 deletion src/SharpMetal/QuartzCore/CAMetalLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ public MTLDevice Device
set => ObjectiveCRuntime.objc_msgSend(NativePtr, sel_setDevice, value);
}

public MTLPixelFormat PixelFormat => (MTLPixelFormat)ObjectiveCRuntime.ulong_objc_msgSend(NativePtr, sel_setPixelFormat);
public MTLPixelFormat PixelFormat
{
get => (MTLPixelFormat)ObjectiveCRuntime.ulong_objc_msgSend(NativePtr, sel_pixelFormat);
set => ObjectiveCRuntime.objc_msgSend(NativePtr, sel_setPixelFormat, (ulong)value);
}

public IntPtr Colorspace
{
get => ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, sel_colorspace);
set => ObjectiveCRuntime.objc_msgSend(NativePtr, sel_setColorspace, value);
}

public bool FramebufferOnly
{
Expand All @@ -35,7 +45,10 @@ public IntPtr DrawableSize
private static readonly Selector sel_layer = "layer";
private static readonly Selector sel_device = "device";
private static readonly Selector sel_setDevice = "setDevice:";
private static readonly Selector sel_pixelFormat = "pixelFormat";
private static readonly Selector sel_setPixelFormat = "setPixelFormat:";
private static readonly Selector sel_colorspace = "colorspace";
private static readonly Selector sel_setColorspace = "setColorspace:";
private static readonly Selector sel_framebufferOnly = "framebufferOnly";
private static readonly Selector sel_setFramebufferOnly = "setFramebufferOnly:";
private static readonly Selector sel_drawableSize = "drawableSize";
Expand Down
78 changes: 78 additions & 0 deletions src/SharpMetal/QuartzCore/CGColorSpace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using SharpMetal.Foundation;
using SharpMetal.ObjectiveCCore;

namespace SharpMetal.QuartzCore
{
public enum ColorSpace
{
DisplayP3,
DisplayP3HLG,
ExtendedLinearDisplayP3,
SRGB,
LinearSRGB,
ExtendedSRGB,
ExtendedLinearSRGB,
GenericGrayGamma22,
LinearGray,
ExtendedGray,
ExtendedLinearGray,
GenericRGBLinear,
GenericCMYK,
GenericXYZ,
GenericLab,
ACESCGLinear,
AdobeRGB1998,
DCIP3,
ITUR709,
ROMMRGB,
ITUR2020,
ExtendedLinearITUR2020
}

[SupportedOSPlatform("macos")]
public partial struct CGColorSpace
{
private const string CoreGraphicsFramework = "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics";

[LibraryImport(CoreGraphicsFramework)]
private static partial IntPtr CGColorSpaceCreateWithName(IntPtr name);

private static NSString GetString(ColorSpace colorSpace)
{
var colorSpaceName = colorSpace switch

Check warning on line 44 in src/SharpMetal/QuartzCore/CGColorSpace.cs

View workflow job for this annotation

GitHub Actions / build / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(SharpMetal.QuartzCore.ColorSpace)22' is not covered.
{
ColorSpace.DisplayP3 => "kCGColorSpaceDisplayP3",
ColorSpace.DisplayP3HLG => "kCGColorSpaceDisplayP3_HLG",
ColorSpace.ExtendedLinearDisplayP3 => "kCGColorSpaceExtendedLinearDisplayP3",
ColorSpace.SRGB => "kCGColorSpaceSRGB",
ColorSpace.LinearSRGB => "kCGColorSpaceLinearSRGB",
ColorSpace.ExtendedSRGB => "kCGColorSpaceExtendedSRGB",
ColorSpace.ExtendedLinearSRGB => "kCGColorSpaceExtendedLinearSRGB",
ColorSpace.GenericGrayGamma22 => "kCGColorSpaceGenericGrayGamma2_2",
ColorSpace.LinearGray => "kCGColorSpaceLinearGray",
ColorSpace.ExtendedGray => "kCGColorSpaceExtendedGray",
ColorSpace.ExtendedLinearGray => "kCGColorSpaceExtendedLinearGray",
ColorSpace.GenericRGBLinear => "kCGColorSpaceGenericRGBLinear",
ColorSpace.GenericCMYK => "kCGColorSpaceGenericCMYK",
ColorSpace.GenericXYZ => "kCGColorSpaceGenericXYZ",
ColorSpace.GenericLab => "kCGColorSpaceGenericLab",
ColorSpace.ACESCGLinear => "kCGColorSpaceACESCGLinear",
ColorSpace.AdobeRGB1998 => "kCGColorSpaceAdobeRGB1998",
ColorSpace.DCIP3 => "kCGColorSpaceDCIP3",
ColorSpace.ITUR709 => "kCGColorSpaceITUR_709",
ColorSpace.ROMMRGB => "kCGColorSpaceROMMRGB",
ColorSpace.ITUR2020 => "kCGColorSpaceITUR_2020",
ColorSpace.ExtendedLinearITUR2020 => "kCGColorSpaceExtendedLinearITUR_2020",
};

return new(ObjectiveC.IntPtr_objc_msgSend(new ObjectiveCClass("NSString"), "stringWithUTF8String:", colorSpaceName));
}

public static IntPtr CreateCGColorSpace(ColorSpace colorSpace)
{
return CGColorSpaceCreateWithName(GetString(colorSpace));
}
}
}
2 changes: 1 addition & 1 deletion src/SharpMetal/SharpMetal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>SharpMetal</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0-preview12</Version>
<Version>1.0.0-preview13</Version>
<Description>Handcrafted Metal bindings for C#</Description>
<RepositoryUrl>https://github.com/IsaacMarovitz/SharpMetal</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down

0 comments on commit 2e1e22e

Please sign in to comment.