@@ -1568,6 +1568,31 @@ class Paint {
15681568 }
15691569}
15701570
1571+ /// The color space describes the colors that are available to an [Image] .
1572+ ///
1573+ /// This value can help decide which [ImageByteFormat] to use with
1574+ /// [Image.toByteData] . Images that are in the [extendedSRGB] color space
1575+ /// should use something like [ImageByteFormat.rawExtendedRgba128] so that
1576+ /// colors outside of the sRGB gamut aren't lost.
1577+ ///
1578+ /// This is also the result of [Image.colorSpace] .
1579+ ///
1580+ /// See also: https://en.wikipedia.org/wiki/Color_space
1581+ enum ColorSpace {
1582+ /// The sRGB color space.
1583+ ///
1584+ /// You may know this as the standard color space for the web or the color
1585+ /// space of non-wide-gamut Flutter apps.
1586+ ///
1587+ /// See also: https://en.wikipedia.org/wiki/SRGB
1588+ sRGB,
1589+ /// A color space that is backwards compatible with sRGB but can represent
1590+ /// colors outside of that gamut with values outside of [0..1] . In order to
1591+ /// see the extended values an [ImageByteFormat] like
1592+ /// [ImageByteFormat.rawExtendedRgba128] must be used.
1593+ extendedSRGB,
1594+ }
1595+
15711596/// The format in which image bytes should be returned when using
15721597/// [Image.toByteData] .
15731598// We do not expect to add more encoding formats to the ImageByteFormat enum,
@@ -1591,6 +1616,31 @@ enum ImageByteFormat {
15911616 /// image may use a single 8-bit channel for each pixel.
15921617 rawUnmodified,
15931618
1619+ /// Raw extended range RGBA format.
1620+ ///
1621+ /// Unencoded bytes, in RGBA row-primary form with straight alpha, 32 bit
1622+ /// float (IEEE 754 binary32) per channel.
1623+ ///
1624+ /// Example usage:
1625+ ///
1626+ /// ```dart
1627+ /// import 'dart:ui' as ui;
1628+ /// import 'dart:typed_data';
1629+ ///
1630+ /// Future<Map<String, double>> getFirstPixel(ui.Image image) async {
1631+ /// final ByteData data =
1632+ /// (await image.toByteData(format: ui.ImageByteFormat.rawExtendedRgba128))!;
1633+ /// final Float32List floats = Float32List.view(data.buffer);
1634+ /// return <String, double>{
1635+ /// 'r': floats[0],
1636+ /// 'g': floats[1],
1637+ /// 'b': floats[2],
1638+ /// 'a': floats[3],
1639+ /// };
1640+ /// }
1641+ /// ```
1642+ rawExtendedRgba128,
1643+
15941644 /// PNG format.
15951645 ///
15961646 /// A loss-less compression format for images. This format is well suited for
@@ -1726,6 +1776,10 @@ class Image {
17261776 /// The [format] argument specifies the format in which the bytes will be
17271777 /// returned.
17281778 ///
1779+ /// Using [ImageByteFormat.rawRgba] on an image in the color space
1780+ /// [ColorSpace.extendedSRGB] will result in the gamut being squished to fit
1781+ /// into the sRGB gamut, resulting in the loss of wide-gamut colors.
1782+ ///
17291783 /// Returns a future that completes with the binary image data or an error
17301784 /// if encoding fails.
17311785 // We do not expect to add more encoding formats to the ImageByteFormat enum,
@@ -1737,6 +1791,29 @@ class Image {
17371791 return _image.toByteData (format: format);
17381792 }
17391793
1794+ /// The color space that is used by the [Image] 's colors.
1795+ ///
1796+ /// This value is a consequence of how the [Image] has been created. For
1797+ /// example, loading a PNG that is in the Display P3 color space will result
1798+ /// in a [ColorSpace.extendedSRGB] image.
1799+ ///
1800+ /// On rendering backends that don't support wide gamut colors (anything but
1801+ /// iOS impeller), wide gamut images will still report [ColorSpace.sRGB] if
1802+ /// rendering wide gamut colors isn't supported.
1803+ // Note: The docstring will become outdated as new platforms support wide
1804+ // gamut color, please keep it up to date.
1805+ ColorSpace get colorSpace {
1806+ final int colorSpaceValue = _image.colorSpace;
1807+ switch (colorSpaceValue) {
1808+ case 0 :
1809+ return ColorSpace .sRGB;
1810+ case 1 :
1811+ return ColorSpace .extendedSRGB;
1812+ default :
1813+ throw UnsupportedError ('Unrecognized color space: $colorSpaceValue ' );
1814+ }
1815+ }
1816+
17401817 /// If asserts are enabled, returns the [StackTrace] s of each open handle from
17411818 /// [clone] , in creation order.
17421819 ///
@@ -1903,6 +1980,9 @@ class _Image extends NativeFieldWrapperClass1 {
19031980
19041981 final Set <Image > _handles = < Image > {};
19051982
1983+ @Native < Int32 Function (Pointer <Void >)> (symbol: 'Image::colorSpace' )
1984+ external int get colorSpace;
1985+
19061986 @override
19071987 String toString () => '[$width \u 00D7$height ]' ;
19081988}
0 commit comments