You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中如何用多层ForEach-Object循环实现三变量迭代生成渐变色条?

PowerShell函数NEW-ANSIBAR-16M实现棕色渐变条问题

我正在编写PowerShell函数NEW-ANSIBAR-16M,用于生成16M色的ANSI颜色条。当前代码仅遍历RANGE_R变量,生成灰色渐变条,需要同时遍历RANGE_R、RANGE_G、RANGE_B三个变量,实现棕色渐变效果(效果示意图如下)。尝试嵌套ForEach-Object循环时出现错误,寻求可行的实现方法。

当前代码及调用示例

FUNCTION NEW-ANSIBAR-16M
{
    [cmdletbinding()]
    PARAM (
            [PARAMETER(Mandatory, HelpMessage = "Enter a range of 16M color values, e.g. (1..255)")]
            [VALIDATENOTNULLOREMPTY()]
            [int[]]
            $RANGE_R,
            [int[]]
            $RANGE_G,
            [int[]]
            $RANGE_B,
            [Parameter(HelpMessage = "How many spaces do you want in the bar? This will increase the length of the bar.")]
            [int]
            $Spacing = 1
    )
    $ESC = "$([CHAR]0X1b)"
    $OUT = @()
    $BLANK = " " * $Spacing
    $out += $RANGE_R | ForEach-Object {         # ◄--- 需要加入$Range_G和$Range_B
        "$esc[48;2;$($_);$($_);$($_);1m$($blank)$esc[0m" 

        # 目前是灰色渐变,因为G和B通道用了和R一样的变量值
    }
    $OUT -JOIN ""
    
}

NEW-ANSIBAR-16M -RANGE_R (1..255) -RANGE_G (100..150) -RANGE_B (100..150) -Spacing 1

目标效果示意图

棕色渐变效果

内容的提问来源于stack exchange,提问作者sylvain gagnon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:22:46