Thread Tools Display Modes
07-27-24, 07:08 PM   #1
x2infinity
A Murloc Raider
Join Date: Apr 2013
Posts: 5
How to use color picker frame in slash command?

Im trying to change the colour of a texture using the Color picker frame

This is my current slash command I'd like to replace with the color picker

Code:
-- Slash command to change color
SLASH_COLORCHANGE1 = "/starcolour"
SlashCmdList["COLORCHANGE"] = function(msg)
    local r, g, b = strsplit(" ", msg)
    r, g, b = tonumber(r), tonumber(g), tonumber(b)
    if r and g and b then
        local alpha = StarCursor.texture:GetAlpha()
        StarCursor.texture:SetVertexColor(r, g, b, alpha)
        StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b = r, g, b
    end
end
Code:
-- Slash command to open color picker
SLASH_COLORPICKER1 = "/starcolour"
SlashCmdList["COLORPICKER"] = function()
    ColorPickerFrame:SetupColorPickerAndShow({
        r = StarCursorSettings.r,
        g = StarCursorSettings.g,
        b = StarCursorSettings.b,
        opacity = StarCursorSettings.alpha,
        hasOpacity = true,
        swatchFunc = function()
            local r, g, b = ColorPickerFrame:GetColorRGB()
            StarCursor.texture:SetVertexColor(r, g, b, a)
            StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b, StarCursorSettings.alpha = r, g, b, a
        end,
        cancelFunc = function()
            -- Do nothing on cancel
        end
    })
end
If I change the color on the wheel and then cancel, it doesnt revert the color it just stays how it was selected on the wheel.

Last edited by x2infinity : 07-27-24 at 07:38 PM.
  Reply With Quote
07-27-24, 08:42 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,974
Lua Code:
  1. -- dummy saved settings
  2. local StarCursorSettings = { r=1, g=1, b=1, opacity=1, }
  3.  
  4. -- dummy frame and texture
  5. local StarCursor = CreateFrame("Frame")
  6. StarCursor:SetSize(20, 20)
  7. StarCursor:SetPoint("TOP")
  8. StarCursor.texture = StarCursor:CreateTexture()
  9. StarCursor.texture:SetAllPoints()
  10. StarCursor.texture:SetColorTexture(1, 1, 1)
  11.  
  12. -- change the colour and save settings
  13. local function SwatchFunction()
  14.     local r, g, b  = ColorPickerFrame:GetColorRGB()
  15.     local opacity = ColorPickerFrame:GetColorAlpha()
  16.     StarCursor.texture:SetVertexColor(r, g, b, opacity)
  17.     StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b, StarCursorSettings.opacity = r, g, b, opacity
  18. end
  19.  
  20. -- reset colour on cancel and save settings
  21. local function CancelFunction()
  22.     local color = ColorPickerFrame.previousValues
  23.     StarCursor.texture:SetVertexColor(color.r, color.g, color.b, color.a)
  24.     StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b, StarCursorSettings.opacity = color.r, color.g, color.b, color.a
  25. end
  26.  
  27. -- slash command
  28. SLASH_COLORPICKER1 = "/starcolour"
  29. SlashCmdList["COLORPICKER"] = function(msg)
  30.     local cpSettings = {}
  31.     for k, v in pairs(StarCursorSettings) do
  32.         cpSettings[k] = v
  33.     end
  34.     cpSettings.hasOpacity = true
  35.         cpSettings.swatchFunc = SwatchFunction
  36.         cpSettings.cancelFunc = CancelFunction
  37.     ColorPickerFrame:SetupColorPickerAndShow(cpSettings)
  38. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 07-27-24 at 08:56 PM.
  Reply With Quote
07-27-24, 09:20 PM   #3
x2infinity
A Murloc Raider
Join Date: Apr 2013
Posts: 5
Thanks I did figure out a solution:

Code:
SLASH_COLORPICKER1 = "/starcolour"
SlashCmdList["COLORPICKER"] = function()
    local prevr, prevg, prevb, preva = StarCursor.texture:GetVertexColor()
    StarCursor.texture:Show();
    ColorPickerFrame:SetupColorPickerAndShow({
        r = StarCursorSettings.r,
        g = StarCursorSettings.g,
        b = StarCursorSettings.b,
        opacity = StarCursorSettings.alpha,
        hasOpacity = true,
        swatchFunc = function()
            local r, g, b = ColorPickerFrame:GetColorRGB()
            local a = ColorPickerFrame:GetColorAlpha()
            StarCursor.texture:SetVertexColor(r, g, b, a)
            StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b, StarCursorSettings.alpha = r, g, b, a
        end,
        cancelFunc = function()
            StarCursor.texture:SetVertexColor(prevr, prevg, prevb, preva)
            StarCursorSettings.r, StarCursorSettings.g, StarCursorSettings.b, StarCursorSettings.alpha = prevr, prevg, prevb, preva
        end
    })
end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to use color picker frame in slash command?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off