excel vba在photoshop中改变颜色

hrysbysz  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(100)

我试图改变一个层的颜色使用vba
基本上,我有一个纹理文件,我想改变它的基础上的细胞值层,因为我需要创建大量的。电子表格上的颜色值为R G B格式。
我有两个版本的PSD文件(可以修改),一个颜色叠加,另一个只是作为一个调整层Fillcolor。
尝试了以下操作,并尝试在对象中查找颜色属性,但找不到>
有什么建议吗?

Sub modify_psd_files()
    'Define variables for Excel and Photoshop objects
    Dim appExcel As Excel.Application
    Dim wbExcel As Excel.Workbook
    Dim wsExcel As Excel.Worksheet
    Dim appPhotoshop As Object 'Declare as Object to use late binding
    Dim docPhotoshop As Object 'Declare as Object to use late binding
    
    'Define variables for the file names and paths
    Dim filePath As String
    Dim fileName As String
    Dim savePath As String
    Dim saveName As String
    
    'Define variables for the layer name and color
    Dim layerName As String
    Dim layerColor As String
    Dim hexColor As String
    Dim r As Integer
    Dim g As Integer
    Dim b As Integer
    
    'Open Excel file and set variables
    Set appExcel = New Excel.Application
    Set wbExcel = ThisWorkbook 'Use the workbook executing the code
    Set wsExcel = wbExcel.Worksheets("PSD") 
    
    'Open Photoshop and set variables
    Set appPhotoshop = CreateObject("Photoshop.Application")
    appPhotoshop.Visible = True 'Set to False to hide Photoshop
    
    'Loop through rows in Excel and modify Photoshop file
    For i = 2 To wsExcel.Cells(wsExcel.Rows.Count, "A").End(xlUp).Row 
        'Get file name, layer name, layer color, and save name from Excel
        fileName = ThisWorkbook.Path & "\" & wsExcel.Cells(i, 1).Value 'Use workbook path as base path for file
        layerName = wsExcel.Cells(i, 2).Value
        hexColor = wsExcel.Cells(i, 3).Value
        saveName = wsExcel.Cells(i, 4).Value
        
        'Open PSD file and set variables
        Set docPhotoshop = appPhotoshop.Open(fileName)
        Dim layer As Object 'Declare as Object to use late binding
        
        'Find layer by name and change color
        For Each layer In docPhotoshop.artLayers
            If layer.Name = layerName Then
                Dim artLayer As Object 'Declare as Object to use late binding
                Set artLayer = layer
                artLayer.ApplyColorOverlay
                artLayer.adjustment.ColorBalance(0) = 100
                artLayer.adjustment.ColorBalance(1) = 100
                artLayer.adjustment.ColorBalance(2) = 100
                Exit For
            End If
        Next layer
        
        'Save modified file with new name
        savePath = Left(fileName, InStrRev(fileName, "\")) 'Get path from original file name
        docPhotoshop.SaveAs savePath & saveName & ".tga"
        docPhotoshop.Close
        
    Next i
    'Close Excel and Photoshop and clean up objects
    'wbExcel.Close
    'appExcel.Quit
    'appPhotoshop.Quit
    'Set wsExcel = Nothing
    'Set wbExcel = Nothing
    'Set appExcel = Nothing

End Sub
ukdjmx9f

ukdjmx9f1#

我提供以下的解决方案给你和任何人与Photoshop和DOM。
使用WordPress的知识在Photoshop中打开所需的PSD文件,搜索solidfill层并激活它。(这你必须自己做)en使用下面的解决方案chgcol()来改变固体填充层的颜色。
在下面的解决方案中,photoshop JavaScript .jsx文件是从xml2调用的。技巧是设置环境变量,我很欣赏这个link
1.创建一个文本文件并给予名称changecolorfill.jsx
1.将以下代码放入其中并保存

#target photoshop

function changeSolidFillColor() {
    var rValue = parseFloat($.getenv('rValue'));
    var gValue = parseFloat($.getenv('gValue'));
    var bValue = parseFloat($.getenv('bValue'));

    if (app.documents.length > 0) {
        if (activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {
            setColorFill(rValue, gValue, bValue);
        } else {
            alert("The active layer isn't a Color Fill layer!");
        }
    } else {
        alert('You must have a document open!');
    }

    function setColorFill(red, green, blue) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var descriptor3 = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(s2t("null"), reference);
        descriptor3.putDouble(s2t("red"), red);
        descriptor3.putDouble(s2t("green"), green);
        descriptor3.putDouble(s2t("blue"), blue);
        descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
        descriptor.putObject(s2t("to"), s2t("solidColorLayer"), descriptor2);
        executeAction(s2t("set"), descriptor, DialogModes.NO);
    }
}

changeSolidFillColor();

1.在photoshop中打开文件,其中将有一个填充颜色层,并使该层可见和活动。
1.在JavaScript模块中,放入以下代码并运行chgcol()子例程。#3f79bd是皇家蓝色十六进制码。
.
谢谢...

Sub chgcol()
    Dim rgbcol As Variant
    rgbcol = HexToRGB("#3f79bd")
    'Debug.Print "Red: " & rgbcol(0) & ", Green: " & rgbcol(1) & ", Blue: " & rgbcol(2)
    Call RunJavaScriptInPhotoshop(CDbl(rgbcol(0)), CDbl(rgbcol(1)), CDbl(rgbcol(2)))
End Sub

Function HexToRGB(hexColor As String) As Variant
    Dim r As Double
    Dim g As Double
    Dim b As Double

    ' Remove the '#' from the start of the string if it exists
    If Left(hexColor, 1) = "#" Then
        hexColor = Right(hexColor, Len(hexColor) - 1)
    End If

    ' Convert the hex color to RGB values
    r = Application.WorksheetFunction.Hex2Dec(Left(hexColor, 2))
    g = Application.WorksheetFunction.Hex2Dec(Mid(hexColor, 3, 2))
    b = Application.WorksheetFunction.Hex2Dec(Right(hexColor, 2))

    ' Return the RGB values as an array
    HexToRGB = Array(r, g, b)
End Function


Sub RunJavaScriptInPhotoshop(rValue As Double, gValue As Double, bValue As Double)
    ' Create a reference to the Photoshop application
    Dim appRef As Photoshop.Application
    Set appRef = CreateObject("Photoshop.Application")
    
 ' Set the environment variables in Photoshop
    Dim jsCode As String
    jsCode = "$.setenv('rValue', '" & rValue & "');"
    jsCode = jsCode & "$.setenv('gValue', '" & gValue & "');"
    jsCode = jsCode & "$.setenv('bValue', '" & bValue & "');"
    appRef.DoJavaScript (jsCode)

    ' Run the JavaScript in Photoshop
    Dim stn As String
    stn = appRef.DoJavaScriptFile("C:\path\to\your\ChangeSolidFillColor.jsx")
End Sub

相关问题