/*
 * 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 {

	/** Functions for blending in HCT and CAM16. */
	public class Blend : Object {
		private Blend() {}

		/**
		 * Blend the design color's HCT hue towards the key color's HCT hue, in a way that leaves the
		 * original color recognizable and recognizably shifted towards the key color.
		 *
		 * @param design_color ARGB representation of an arbitrary color.
		 * @param sourceColor ARGB representation of the main theme color.
		 * @return The design color with a hue shifted towards the system's color, a slightly
		 *     warmer/cooler variant of the design color's hue.
		 */
		public static int harmonize(int design_color, int source_color) {
			Hct from_hct = Hct.from_int(design_color);
			Hct to_hct = Hct.from_int(source_color);

			double difference_degrees = MathUtils.difference_degrees(from_hct.hue, to_hct.hue);
			double rotation_degrees = Math.fmin(difference_degrees * 0.5, 15.0);

			double output_hue = MathUtils.sanitize_degrees_double(from_hct.hue + rotation_degrees * MathUtils.rotation_direction(from_hct.hue, to_hct.hue));

			return Hct.from(output_hue, from_hct.chroma, from_hct.tone).to_int();
		}

		/**
		 * Blends hue from one color into another. The chroma and tone of the original color are
		 * maintained.
		 *
		 * @param from ARGB representation of color
		 * @param to ARGB representation of color
		 * @param amount how much blending to perform; 0.0 >= and <= 1.0
		 * @return from, with a hue blended towards to. Chroma and tone are constant.
		 */
		public static int hct_hue(int from, int to, double amount) {
			int ucs = cam16_ucs(from, to, amount);
			Cam16 ucs_cam = Cam16.from_int(ucs);
			Cam16 from_cam = Cam16.from_int(from);
			Hct blended = Hct.from(ucs_cam.hue, from_cam.chroma, ColorUtils.lstar_from_argb(from));
			return blended.to_int();
		}

		/**
		 * Blend in CAM16-UCS space.
		 *
		 * @param from ARGB representation of color
		 * @param to ARGB representation of color
		 * @param amount how much blending to perform; 0.0 >= and <= 1.0
		 * @return from, blended towards to. Hue, chroma, and tone will change.
		 */
		public static int cam16_ucs(int from, int to, double amount) {
			Cam16 from_cam = Cam16.from_int(from);
			Cam16 to_cam = Cam16.from_int(to);
			double jstar = MathUtils.lerp(from_cam.jstar, to_cam.jstar, amount);
			double astar = MathUtils.lerp(from_cam.astar, to_cam.astar, amount);
			double bstar = MathUtils.lerp(from_cam.bstar, to_cam.bstar, amount);
			return Cam16.from_ucs(jstar, astar, bstar).to_int();
		}
	}
}
