/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ---------------------------------------------------------------------
 *
 * Portions of this file were modified and ported to Vala by Victor Vintorez in 2026.
 * These modifications are licensed under the European Union Public License (EUPL),
 * Version 1.2 or later. You may obtain a copy of the License at:
 * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
 */

using GLib;

namespace MaterialColorUtilities {
	/**
	 * A class containing a value that changes with the contrast level.
	 *
	 * <p>Usually represents the contrast requirements for a dynamic color on its background. The four
	 * values correspond to values for contrast levels -1.0, 0.0, 0.5, and 1.0, respectively.
	 */
	public sealed class ContrastCurve : Object {
		/** Value for contrast level -1.0 */
		private double low;

		/** Value for contrast level 0.0 */
		private double normal;

		/** Value for contrast level 0.5 */
		private double medium;

		/** Value for contrast level 1.0 */
		private double high;

		/**
		 * Creates a `ContrastCurve` object.
		 *
		 * @param low Value for contrast level -1.0
		 * @param normal Value for contrast level 0.0
		 * @param medium Value for contrast level 0.5
		 * @param high Value for contrast level 1.0
		 */
		public ContrastCurve (double low, double normal, double medium, double high) {
			this.low = low;
			this.normal = normal;
			this.medium = medium;
			this.high = high;
		}

		/**
		 * Returns the value at a given contrast level.
		 *
		 * @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0 is the lowest; 1.0
		 *     is the highest.
		 * @return The value. For contrast ratios, a number between 1.0 and 21.0.
		 */
		public double get_value (double contrast_level) {
			if (contrast_level <= -1.0) {
				return this.low;
			} else if (contrast_level < 0.0) {
				return MathUtils.lerp (this.low, this.normal, contrast_level + 1.0);
			} else if (contrast_level < 0.5) {
				return MathUtils.lerp (this.normal, this.medium, (contrast_level - 0) / 0.5);
			} else if (contrast_level < 1.0) {
				return MathUtils.lerp (this.medium, this.high, (contrast_level - 0.5) / 0.5);
			} else {
				return this.high;
			}
		}
	}
}
