array( 0.4360747, 0.2225045, 0.0139322 ), 'g' => array( 0.3850649, 0.7168786, 0.0971045 ), 'b' => array( 0.1430804, 0.0606169, 0.7141733 ), ); /** * Cached sRGB profile bytes. * * @var string|null */ private static ?string $srgb = null; /** * Whether a profile describes sRGB, judged by its description tag. * * @param string $profile Raw ICC bytes. * @return bool */ public static function is_srgb( string $profile ): bool { $description = self::description( $profile ); return '' !== $description && false !== stripos( $description, 'srgb' ); } /** * Reads a profile's description tag as plain text. * * Handles both v2 `desc` (ASCII) and v4 `mluc` (UTF-16BE) by discarding NUL bytes. * * @param string $profile Raw ICC bytes. * @return string */ public static function description( string $profile ): string { if ( strlen( $profile ) < 132 ) { return ''; } $count = unpack( 'Ncount', substr( $profile, 128, 4 ) ); $count = false === $count ? 0 : (int) $count['count']; $limit = min( $count, 100 ); for ( $i = 0; $i < $limit; $i++ ) { $entry = substr( $profile, 132 + 12 * $i, 12 ); if ( strlen( $entry ) < 12 ) { return ''; } if ( 'desc' !== substr( $entry, 0, 4 ) ) { continue; } $fields = unpack( 'Noffset/Nsize', substr( $entry, 4, 8 ) ); if ( false === $fields ) { return ''; } $payload = substr( $profile, (int) $fields['offset'], (int) $fields['size'] ); return str_replace( "\0", '', substr( $payload, 8, 256 ) ); } return ''; } /** * Returns a standard sRGB profile, generated here so no third-party file ships with the plugin. * * @return string */ public static function srgb(): string { if ( null === self::$srgb ) { self::$srgb = self::build( 'sRGB (PixelKeep)', self::SRGB_COLORANTS, self::srgb_curve() ); } return self::$srgb; } /** * Returns a wide-gamut profile with Adobe RGB colorants, used to test color conversion. * * @return string */ public static function wide_gamut(): string { return self::build( 'Wide Gamut Test', array( 'r' => array( 0.60974, 0.31111, 0.01947 ), 'g' => array( 0.20528, 0.62567, 0.06087 ), 'b' => array( 0.14919, 0.06322, 0.74457 ), ), self::gamma_curve( 2.2 ) ); } /** * Builds an ICC v2 RGB display profile. * * @param string $description Profile name. * @param array> $colorants D50-adapted XYZ for r, g, b. * @param string $curve Tone curve tag payload. * @return string */ public static function build( string $description, array $colorants, string $curve ): string { $tags = array( 'desc' => self::description_tag( $description ), 'cprt' => "text\0\0\0\0Public Domain\0\0\0", 'wtpt' => self::xyz_tag( array( 0.9642, 1.0, 0.8249 ) ), 'rXYZ' => self::xyz_tag( $colorants['r'] ), 'gXYZ' => self::xyz_tag( $colorants['g'] ), 'bXYZ' => self::xyz_tag( $colorants['b'] ), 'rTRC' => $curve, 'gTRC' => $curve, 'bTRC' => $curve, ); $table_size = 4 + 12 * count( $tags ); $offset = 128 + $table_size; $table = pack( 'N', count( $tags ) ); $body = ''; $shared = array(); foreach ( $tags as $signature => $payload ) { if ( ! isset( $shared[ $payload ] ) ) { $shared[ $payload ] = $offset + strlen( $body ); $body .= $payload . str_repeat( "\0", ( 4 - strlen( $payload ) % 4 ) % 4 ); } $table .= $signature . pack( 'NN', $shared[ $payload ], strlen( $payload ) ); } $header = pack( 'N', 128 + $table_size + strlen( $body ) ); $header .= "\0\0\0\0"; $header .= pack( 'N', 0x02100000 ); $header .= 'mntrRGB XYZ '; $header .= pack( 'nnnnnn', 2026, 1, 1, 0, 0, 0 ); $header .= 'acsp'; $header .= str_repeat( "\0", 4 + 4 + 4 + 4 + 8 + 4 ); $header .= self::fixed( 0.9642 ) . self::fixed( 1.0 ) . self::fixed( 0.8249 ); $header .= 'PXFG'; $header .= str_repeat( "\0", 128 - strlen( $header ) ); return $header . $table . $body; } /** * Builds the sRGB tone curve as a 1024-point table. * * @return string */ public static function srgb_curve(): string { $points = 1024; $curve = "curv\0\0\0\0" . pack( 'N', $points ); for ( $i = 0; $i < $points; $i++ ) { $value = $i / ( $points - 1 ); $linear = $value <= 0.04045 ? $value / 12.92 : pow( ( $value + 0.055 ) / 1.055, 2.4 ); $curve .= pack( 'n', (int) round( $linear * 65535 ) ); } return $curve; } /** * Builds a pure gamma tone curve. * * @param float $gamma Exponent. * @return string */ public static function gamma_curve( float $gamma ): string { return "curv\0\0\0\0" . pack( 'N', 1 ) . pack( 'n', (int) round( $gamma * 256 ) ); } /** * Encodes a v2 textDescriptionType tag. * * @param string $text Description. * @return string */ private static function description_tag( string $text ): string { $ascii = $text . "\0"; return "desc\0\0\0\0" . pack( 'N', strlen( $ascii ) ) . $ascii . str_repeat( "\0", 4 + 4 + 2 + 1 + 67 ); } /** * Encodes an XYZ tag. * * @param array $xyz X, Y and Z. * @return string */ private static function xyz_tag( array $xyz ): string { return "XYZ \0\0\0\0" . self::fixed( $xyz[0] ) . self::fixed( $xyz[1] ) . self::fixed( $xyz[2] ); } /** * Encodes a number as s15Fixed16. * * @param float $value Number. * @return string */ private static function fixed( float $value ): string { return pack( 'N', (int) round( $value * 65536 ) & 0xFFFFFFFF ); } }