// Copyright 2022 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

// ignore_for_file: avoid_renaming_method_parameters

import 'dart:typed_data';

import 'package:meta/meta.dart';

import '../../../util/nullable.dart';
import '../../color.dart';
import '../conversions.dart';
import 'utils.dart';

/// The display-p3 color space.
///
/// https://www.w3.org/TR/css-color-4/#predefined-display-p3
///
/// @nodoc
@internal
final class DisplayP3ColorSpace extends ColorSpace {
  bool get isBoundedInternal => true;

  const DisplayP3ColorSpace() : super('display-p3', rgbChannels);

  SassColor convert(ColorSpace dest, double? red, double? green, double? blue,
          double? alpha) =>
      dest == ColorSpace.displayP3Linear
          ? SassColor.forSpaceInternal(
              dest,
              red.andThen(toLinear),
              green.andThen(toLinear),
              blue.andThen(toLinear),
              alpha,
            )
          : super.convertLinear(dest, red, green, blue, alpha);

  @protected
  double toLinear(double channel) => srgbAndDisplayP3ToLinear(channel);

  @protected
  double fromLinear(double channel) => srgbAndDisplayP3FromLinear(channel);

  @protected
  Float64List transformationMatrix(ColorSpace dest) => switch (dest) {
        ColorSpace.srgbLinear ||
        ColorSpace.srgb ||
        ColorSpace.rgb =>
          linearDisplayP3ToLinearSrgb,
        ColorSpace.a98Rgb => linearDisplayP3ToLinearA98Rgb,
        ColorSpace.prophotoRgb => linearDisplayP3ToLinearProphotoRgb,
        ColorSpace.rec2020 => linearDisplayP3ToLinearRec2020,
        ColorSpace.xyzD65 => linearDisplayP3ToXyzD65,
        ColorSpace.xyzD50 => linearDisplayP3ToXyzD50,
        ColorSpace.lms => linearDisplayP3ToLms,
        _ => super.transformationMatrix(dest),
      };
}
