
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

import { colorToRgba } from 'tvrender/lib/core/util.js';
var round = Math.round;
// function parseColor(color: string): [number, number, number] {
//     // 移除可能存在的#号
//     if (color.startsWith('#')) {
//         color = color.slice(1);
//         if (color.length === 3) {
//             // 转换为6位表示
//             color = color.split('').map(c => c + c).join('');
//         }
//         if (color.length !== 6 || !/^[0-9A-Fa-f]{6}$/.test(color)) {
//             return [0, 0, 0];
//         }
//         return [
//             parseInt(color.slice(0, 2), 16),
//             parseInt(color.slice(2, 4), 16),
//             parseInt(color.slice(4, 6), 16),
//         ];
//     }
//     // 匹配RGB或RGBA格式
//     const match = color.match(/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(?:\s*,\s*[\d.]+)?\s*\)/);
//     if (!match) {
//         return [0, 0, 0];
//     }
//     return [
//         parseInt(match[1], 10),
//         parseInt(match[2], 10),
//         parseInt(match[3], 10),
//     ];
// }
function parseColor(color) {
  color = color.trim().toLowerCase();
  // Match rgb() or rgba()
  var rgbRegex = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})/;
  var rgbMatch = color.match(rgbRegex);
  if (rgbMatch) {
    return [parseInt(rgbMatch[1], 10), parseInt(rgbMatch[2], 10), parseInt(rgbMatch[3], 10)];
  }
  // Match hex color
  if (color.startsWith('#')) {
    var hex = color.slice(1);
    // Handle short forms (#rgb, #rgba)
    if (hex.length === 3 || hex.length === 4) {
      hex = hex.split('').map(function (c) {
        return c + c;
      }).join('');
    }
    // Now hex is 6 or 8 digits
    if (hex.length === 6 || hex.length === 8) {
      var r = parseInt(hex.slice(0, 2), 16);
      var g = parseInt(hex.slice(2, 4), 16);
      var b = parseInt(hex.slice(4, 6), 16);
      return [r, g, b];
    }
  }
  return [0, 0, 0];
}
function contrastColor(color, lightweight) {
  if (lightweight === void 0) {
    lightweight = 130;
  }
  var _a = parseColor(color),
    r = _a[0],
    g = _a[1],
    b = _a[2];
  var brightness = (r * 299 + g * 587 + b * 114) / 1000;
  return brightness >= lightweight ? '#000000' : '#ffffff';
}
function getIsLightColor(color) {
  var _a = parseColor(color),
    r = _a[0],
    g = _a[1],
    b = _a[2];
  var brightness = (r * 299 + g * 587 + b * 114) / 1000;
  return brightness >= 130;
}
function removeTransparency(color) {
  var _a = parseColor(color),
    r = _a[0],
    g = _a[1],
    b = _a[2];
  return "rgb(" + r + ", " + g + ", " + b + ")";
}
/**
 * Manhattan 距离法：通道差值绝对值之和
 *
 * @param r1,g1,b1,a1  第一个颜色通道 0–255
 * @param r2,g2,b2,a2  第二个颜色通道 0–255
 * @param threshold01  阈值 0–1，默认 0.1 -> 每通道平均25.5，总和 102
 * @param weightAlpha  Alpha 权重，默认 1.0
 */
function isColorSimilarManhattan(rgbas1, rgbas2, threshold01, weightAlpha) {
  if (threshold01 === void 0) {
    threshold01 = 0.1;
  }
  if (weightAlpha === void 0) {
    weightAlpha = 1.0;
  }
  // 阈值从 [0,1] 转到 [0,255]，并乘以4（最坏情况所有通道都满差）
  var maxSum = threshold01 * 255 * 4;
  // 累加四通道的绝对差
  var sum = Math.abs(rgbas1[0] - rgbas2[0]) + Math.abs(rgbas1[1] - rgbas2[1]) + Math.abs(rgbas1[2] - rgbas2[2]) + Math.abs((rgbas1[3] - rgbas2[3]) * weightAlpha);
  return sum <= maxSum;
}
/**
 * 混合两个颜色（带透明度的前景色与背景色）
 * @param foreground 前景色（RGBA或十六进制格式）
 * @param background 背景色（RGBA或十六进制格式）
 * @returns 混合后的颜色（十六进制格式）
 */
function blendColors(foreground, background) {
  // 解析前景色
  var fg = parseColorToRgba(foreground);
  // 解析背景色
  var bg = parseColorToRgba(background);
  // 确保透明度值存在，默认为1（完全不透明）
  var fgAlpha = fg.a !== undefined ? fg.a : 1;
  var bgAlpha = bg.a !== undefined ? bg.a : 1;
  // Alpha合成公式
  var alpha = fgAlpha + bgAlpha * (1 - fgAlpha);
  var r = Math.round((fg.r * fgAlpha + bg.r * bgAlpha * (1 - fgAlpha)) / alpha);
  var g = Math.round((fg.g * fgAlpha + bg.g * bgAlpha * (1 - fgAlpha)) / alpha);
  var b = Math.round((fg.b * fgAlpha + bg.b * bgAlpha * (1 - fgAlpha)) / alpha);
  // 返回十六进制格式
  return rgbToHex(r, g, b);
}
/**
* 解析颜色字符串为RGB值
* @param color 颜色字符串（RGBA或十六进制格式）
* @returns 包含r、g、b、a的对象
*/
function parseColorToRgba(color) {
  color = color.trim().toLowerCase();
  // 处理RGBA格式
  if (color.startsWith('rgba')) {
    var match = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
    if (match) {
      return {
        r: parseInt(match[1]),
        g: parseInt(match[2]),
        b: parseInt(match[3]),
        a: match[4] ? parseFloat(match[4]) : undefined
      };
    }
  }
  // 处理十六进制格式
  if (color.startsWith('#')) {
    // 移除#号
    color = color.substring(1);
    // 处理缩写形式
    if (color.length === 3) {
      color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
    }
    // 解析RGB值
    var r = parseInt(color.substring(0, 2), 16);
    var g = parseInt(color.substring(2, 4), 16);
    var b = parseInt(color.substring(4, 6), 16);
    var result = {
      r: r,
      g: g,
      b: b
    };
    // 如果有透明度值
    if (color.length === 8) {
      result.a = parseInt(color.substring(6, 8), 16) / 255;
    }
    return result;
  }
  throw new Error("\u65E0\u6CD5\u89E3\u6790\u7684\u989C\u8272\u683C\u5F0F: " + color);
}
/**
* 将RGB值转换为十六进制字符串
* @param r 红色值 (0-255)
* @param g 绿色值 (0-255)
* @param b 蓝色值 (0-255)
* @returns 十六进制颜色字符串
*/
function rgbToHex(r, g, b) {
  return "#" + Math.round(r).toString(16).padStart(2, '0') + Math.round(g).toString(16).padStart(2, '0') + Math.round(b).toString(16).padStart(2, '0');
}
/**
 * 获取渐变色中指定比例的颜色
 */
function getGradientColorByY(color, y) {
  if (typeof color === 'string' || color.type !== 'linear') {
    return color;
  }
  var colorStops = color.colorStops,
    y2 = color.y2;
  var color1 = colorStops[0].color,
    color2 = colorStops[1].color;
  // 限制 ratio 在 [0,1] 区间
  var t = Math.min(1, Math.max(0, y / y2));
  var c1 = parseColorToRgba(color1);
  var c2 = parseColorToRgba(color2);
  // 线性插值
  var r = c1.r + (c2.r - c1.r) * t;
  var g = c1.g + (c2.g - c1.g) * t;
  var b = c1.b + (c2.b - c1.b) * t;
  var a = c1.a ? c1.a + (c2.a - c1.a) * t : 1;
  return "rgba(" + round(r) + ", " + round(g) + ", " + round(b) + ", " + a.toFixed(2) + ")";
}
export { colorToRgba, removeTransparency, contrastColor, isColorSimilarManhattan, blendColors, getIsLightColor, getGradientColorByY };