Old 10-23-2020, 03:38 AM   #1
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default Colorize!


There are already a lot of Track/Item coloring scripts out there. What makes this script differs is its limitation. There are no preset, no customizable palette colors. However you can set how many row and column you need for your palette.


The colors are generated from white to black, and all degree of hue. By default the script provided 11 row 48 column of colors. That's is 528 colors in total. Which i think sufficient for most project. If you need more or less shades just change the number of rows and/or column.


The script are designed to be used with very minimum mouse click. It will detect your last click selection, whether Track or Item. Launch the script, pick the color with hovering your mouse cursor over the palette while instantaneously view it colorize your Item/Track. Left click on the last color you pick will be applied to your Track/Item selection. And the script will automatically closed.

The indicator on the top of the palette, will show you the last selection for the color to be applied. If you wish to cancel without applying any colors, just press the ESC button on your keyboard.


To apply gradient color from two colors, pick the first color with mouse Right-Click and second color with Left-Click.

Available via ReaPack from my github repo
Code:
https://raw.githubusercontent.com/jrengmusic/ReaScript/master/index.xml
__________________
JRENG! | M E T R I C

Last edited by jrengmusic; 10-23-2020 at 03:55 AM.
jrengmusic is offline   Reply With Quote
Old 10-23-2020, 03:58 AM   #2
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 546
Default

...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:111: attempt to perform bitwise operation on a nil value (global 'set_color')

When I launch script and select midi item

...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:105: bad argument #1 to 'ColorToNative' (number has no integer representation)

When selecting track


Win 10, latest build

Reaper 6.15
Skorobagatko is offline   Reply With Quote
Old 10-23-2020, 04:02 AM   #3
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Quote:
Originally Posted by Skorobagatko View Post
...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:111: attempt to perform bitwise operation on a nil value (global 'set_color')

When I launch script and select midi item

...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:105: bad argument #1 to 'ColorToNative' (number has no integer representation)

When selecting track


Win 10, latest build

Reaper 6.15
I thought i've already fix that problem. You may want to try to redownload the script.

__________________
JRENG! | M E T R I C

Last edited by jrengmusic; 10-23-2020 at 12:56 PM.
jrengmusic is offline   Reply With Quote
Old 10-23-2020, 04:05 AM   #4
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 546
Default

Quote:
Originally Posted by jrengmusic View Post
I thought i've already fix that problem. You may want to try to redownload the script.
I added your reapack rep. and synced packages, just tried to sync again - same problem.
Skorobagatko is offline   Reply With Quote
Old 10-23-2020, 04:16 AM   #5
bFooz
Human being with feelings
 
Join Date: Jul 2010
Location: Slovakia
Posts: 2,588
Default

Hi, thanks for the script!

Here the problem:
Code:
...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:105: bad argument #1 to 'ColorToNative' (number has no integer representation)

When I select a track first and then run the script, it says "Nothing selected" so I have to re-select the tracks which is unexpected.


A useful feature could be to show color of currently selected tracks:
1 - I select tracks with the same or different colors
2 - run the script
3 - the colors of selected tracks are indicated on the palette
4 - choose color
This would be useful if you want to match the color of several tracks to a one particular already colored track. You just select all tracks in question and instantly see what color it is you have to use.
bFooz is online now   Reply With Quote
Old 10-23-2020, 04:20 AM   #6
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Quote:
Originally Posted by bFooz View Post

Here the problem:
Code:
...aming\REAPER\Scripts\JRENG!\Utility\JRENG! Colorize!.lua:105: bad argument #1 to 'ColorToNative' (number has no integer representation)
Thanks for pointing that out.

But why the line is different from my latest commit?

Quote:
Originally Posted by bFooz View Post
When I select a track first and then run the script, it says "Nothing selected" so I have to re-select the tracks which is unexpected.
Yeah this happened when you run it from Action List. I would put the script on a toolbar or assign a keyboard shortcut so it could detect the selection properly as intended.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-23-2020, 04:24 AM   #7
bFooz
Human being with feelings
 
Join Date: Jul 2010
Location: Slovakia
Posts: 2,588
Default

From design point of view, I'd suggest to remove the border of each color swatch. Squares are already separated by their color and by 1px space in between them. I would maybe keep the border around the very darkest swatches.
bFooz is online now   Reply With Quote
Old 10-23-2020, 04:27 AM   #8
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Personally i prefer border because it points out the exact color i want to choose.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-23-2020, 05:37 AM   #9
bFooz
Human being with feelings
 
Join Date: Jul 2010
Location: Slovakia
Posts: 2,588
Default

Maybe a few things for consideration:

1 - distribute hue like this
Code:
local xx = math.exp(1.071)
local hx = ((xx^h)-1)/(xx-1)
h = hx
It provides more natural (at least for me) hue distribution, where oranges+yellows get more swatches. And when hue number is set to 4, there are 4 basic colors drawn - red, yellow, green, blue

2 - start the palette with red maybe

bFooz is online now   Reply With Quote
Old 10-24-2020, 01:39 PM   #10
sonictim
Human being with feelings
 
sonictim's Avatar
 
Join Date: Feb 2020
Location: Los Angeles
Posts: 463
Default

This is a really great script! but.. I too am getting this error constantly (making it unusable). It absolutely will not work on items for me as this error pops up the instant I select one. Tracks work but only sometimes

Code:
...pport/REAPER/Scripts/JRENG!/Utility/JRENG! Colorize!.lua:105: bad argument #1 to 'ColorToNative' (number has no integer representation)

I like how it instantly sets the color as you hover over the colors, however, as soon as I drag off the color I like to go change my selection for a new set of tracks, it obviously will change color on me. And if I click it to confirm, it closes the plugin, and then I have to reopen it. Which is fine, but not something I want to be constantly doing.

I'd love it if there were some kind of option to prevent closing the plugin on color selection (left click). Perhaps instead, when left clicking, it could simply lock the color to your selection until you either make a new selection (then auto color is restored) or if you click a different color and the selection is still the same, it could update to this new color.

Perhaps you could have a little tickbox somewhere in your GUI that says "close after color selection" that is enabled by default, but if unticked, it would behave as I suggested above. Then a user could leave it open as a little color pallet for quickly updating colors...

Also, I really like the suggestion about somehow showing the current selected color for matching... that's something I use in protools all the time.

Looking forward to using this one more once you get your bug fix sorted out!
sonictim is offline   Reply With Quote
Old 10-24-2020, 03:46 PM   #11
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

My latest commit is fine on my system. No problem whatsoever.

Could anybody upload the buggy script here? I would like to compare to my latest commit.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-24-2020, 04:46 PM   #12
sonictim
Human being with feelings
 
sonictim's Avatar
 
Join Date: Feb 2020
Location: Los Angeles
Posts: 463
Default

Quote:
Originally Posted by jrengmusic View Post
My latest commit is fine on my system. No problem whatsoever.

Could anybody upload the buggy script here? I would like to compare to my latest commit.

Code:
--[[
 * ReaScript Name: Colorize!
 * About: Track/Item coloring palette instantaneously with minimum click
 * Screenshot: 
 * Author: JRENG!
 * Author URI: http://jrengmusic.com
 * Repository: GitHub > jrengmusic > ReaScript
 * Repository URI: https://github.com/jrengmusic/ReaScript
 * Licence: WTFPL
 * Forum Thread: JRENG! Colorize!
 * Forum Thread URI: 
 * REAPER: 5.0
 * Version: 1.0
--]]
 
--[[
 * Changelog:
 * v1.0 (2020-10-23)
  # Public release

--]]

grad = false
----------------------- USER CONFIG --------------------
row = 11
col = 48
--------------------------------------------------------

function toolbar ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,1)
  reaper.RefreshToolbar2(sec,cmd)
  reaper.atexit(exit)
end

function exit ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,0)
  reaper.RefreshToolbar2(sec,cmd)
  gfx.quit()
end

function round(n)
  return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end

function HSLtoRGB(h,s,l)
    local r, g, b
    if s == 0 then
      r, g, b = l, l, l -- achromatic
    else
      function hue2rgb(p, q, t)
          if t < 0 then t = t + 1 end
          if t > 1 then t = t - 1 end
          if t < .166 then return p + (q - p) * 6 * t end
          if t < 0.5 then return q end
          if t < .666 then return p + (q - p) * (.666 - t) * 6 end
          return p
      end
      local q
        if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
        local p = 2 * l - q
        r = hue2rgb(p, q, h + .333)
        g = hue2rgb(p, q, h)
        b = hue2rgb(p, q, h - .333)
    end
    return r, g, b
end

function get_context ()
  countSelTrack=reaper.CountSelectedTracks(0)
  countSelItem=reaper.CountSelectedMediaItems(0)
  if mouseContext==0 and countSelTrack > 0 then
    trackSet=1 itemset=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif mouseContext==1 and countSelItem > 0 then
    itemSet=1 trackSet=0
    mouse_info=countSelItem.." ".."ITEM SELECTED"
  elseif mouseContext==1 and countSelItem == 0 and countSelTrack>0 then
    trackSet=1 itemSet=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif countSelTrack==0 and countSelItem == 0 then
    trackSet=0 itemSet=0
    mouse_info="NOTHING SELECTED"
  end
end

function map(value, iMin, iMax, oMin, oMax) local n, v
  n = ((value - iMin) / (iMax - iMin))
  v = oMin + n * (oMax-oMin)
  return v
end

function setColor()
    if trackSet == 1 then
      for i = 0, countSelTrack-1 do
        track = reaper.GetSelectedTrack(0, i)
        local r1, g1, b1 = reaper.ColorFromNative(color1)
        local r2, g2, b2 = reaper.ColorFromNative(color2)
        local r = round(map(i, 0, countSelTrack-1, r1, r2))
        local g = round(map(i, 0, countSelTrack-1, g1, g2))
        local b = round(map(i, 0, countSelTrack-1, b1, b2))
        if r and g and b then reaper.SetTrackColor (track, reaper.ColorToNative(r, g, b)) end
      end
    reaper.Undo_EndBlock("Colorize! track", -1)
    elseif itemSet == 1 then
      for i = 0 , countSelItem-1 do
      item = reaper.GetSelectedMediaItem(0, i)
      reaper.SetMediaItemInfo_Value(item, "I_CUSTOMCOLOR", set_color|16777216)
      reaper.UpdateItemInProject(item)
      end
    end
  reaper.TrackList_AdjustWindows(false)
end

function draw_palette(_) local  offset, pal_w
  count = 0
  offset = 2
  pal_w = round(gfx.w - offset)
  for i = 1, col, 1  do
    x = round((i-1) * (pal_w / col) + offset)
    w = round((pal_w /col)- 0.5 * offset)
    h = 20     
    
    for j = 1, row, 1 do
      local red, green, blue, alpha
      if j == 1 then
        red,green,blue=HSLtoRGB(0, 0, 1-i/col) 
      else
        red,green,blue=HSLtoRGB(i/(col), 1, (row-j+1)/row)
      end
        y = round (j * (pal_w / col) + offset)
        gfx.set(red, green, blue, 1)
        gfx.rect (x, y, w, h, 1)
        --border          
        if gfx.mouse_x >= x and gfx.mouse_y >= y and gfx.mouse_x <= x + w and gfx.mouse_y <= y + h then
          for k = 0, 2, 1 do
            gfx.set(0, 0, 0, 0.75 - 0.25 * k)
            gfx.rect (x + k, y + k, w - 2 * k, h - 2 * k, 0)
            local int_r = round(255 * red)
            local int_g = round(255 * green)
            local int_b = round(255 * blue)
            color2 = reaper.ColorToNative(int_r, int_g, int_b) -- integer colour value
            if gfx.mouse_cap == 2 then 
              grad = true
              color1 =  reaper.ColorToNative(int_r, int_g, int_b)
            end
            if grad == false then color1 = color2 end
            setColor()
          end
        else
          gfx.set(0.25, 0.25, 0.25, 0.25)
          gfx.rect (x, y, w, h, 0)
          gfx.set(0, 0, 0, 0.25)
          gfx.rect (x+1, y+1, w-2, h-2, 0) 
        end
    end
  end
end

function window_at_center (title, w, h)
  local l, t, r, b = 0, 0, w, h
  local __, __, screen_w, screen_h = reaper.my_getViewport(l, t, r, b, l, t, r, b, 1)
  local x, y = (screen_w - w) / 2, (screen_h - h) / 2
  gfx.init(title, w, h, 0, x, y)
end

function abort ()
  reaper.Main_OnCommand(40029, 0) -- Undo
  reaper.atexit(exit)
end

function draw()
  mouseContext = reaper.GetCursorContext()
  if mouse_info == nil then mouse_info = 'NOTHING SELECTED' end
  toolbar()
  local width = col * 22 + 2
  local height = (row + 1) * 22 + 2
  window_at_center("Colorize!", width, height)
   
  get_context()
  draw_palette()
  
  gfx.set(0, 1, 1, 0.125)
  gfx.rect(4, 4, gfx.w-8, 16, 1)
  gfx.set(0.25, 1, 1);
  str_w, str_h = gfx.measurestr(mouse_info);
  gfx.x = (gfx.w-str_w)/2;
  gfx.y = 8;
  gfx.drawstr(mouse_info);
  
  local c=gfx.getchar()
  if c == 27 then abort() end
  if gfx.mouse_cap ~= 1 and c~= 27 then reaper.defer(draw) else exit() end -- ESC to exit
  gfx.update()
  
end

function main()
  reaper.Undo_BeginBlock()
  draw()
  reaper.Undo_EndBlock("Colorize! item", -1)
end

main()
sonictim is offline   Reply With Quote
Old 10-24-2020, 05:41 PM   #13
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Quote:
Originally Posted by sonictim View Post
Code:
--[[
 * ReaScript Name: Colorize!
 * About: Track/Item coloring palette instantaneously with minimum click
 * Screenshot: 
 * Author: JRENG!
 * Author URI: http://jrengmusic.com
 * Repository: GitHub > jrengmusic > ReaScript
 * Repository URI: https://github.com/jrengmusic/ReaScript
 * Licence: WTFPL
 * Forum Thread: JRENG! Colorize!
 * Forum Thread URI: 
 * REAPER: 5.0
 * Version: 1.0
--]]
 
--[[
 * Changelog:
 * v1.0 (2020-10-23)
  # Public release

--]]

grad = false
----------------------- USER CONFIG --------------------
row = 11
col = 48
--------------------------------------------------------

function toolbar ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,1)
  reaper.RefreshToolbar2(sec,cmd)
  reaper.atexit(exit)
end

function exit ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,0)
  reaper.RefreshToolbar2(sec,cmd)
  gfx.quit()
end

function round(n)
  return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end

function HSLtoRGB(h,s,l)
    local r, g, b
    if s == 0 then
      r, g, b = l, l, l -- achromatic
    else
      function hue2rgb(p, q, t)
          if t < 0 then t = t + 1 end
          if t > 1 then t = t - 1 end
          if t < .166 then return p + (q - p) * 6 * t end
          if t < 0.5 then return q end
          if t < .666 then return p + (q - p) * (.666 - t) * 6 end
          return p
      end
      local q
        if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
        local p = 2 * l - q
        r = hue2rgb(p, q, h + .333)
        g = hue2rgb(p, q, h)
        b = hue2rgb(p, q, h - .333)
    end
    return r, g, b
end

function get_context ()
  countSelTrack=reaper.CountSelectedTracks(0)
  countSelItem=reaper.CountSelectedMediaItems(0)
  if mouseContext==0 and countSelTrack > 0 then
    trackSet=1 itemset=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif mouseContext==1 and countSelItem > 0 then
    itemSet=1 trackSet=0
    mouse_info=countSelItem.." ".."ITEM SELECTED"
  elseif mouseContext==1 and countSelItem == 0 and countSelTrack>0 then
    trackSet=1 itemSet=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif countSelTrack==0 and countSelItem == 0 then
    trackSet=0 itemSet=0
    mouse_info="NOTHING SELECTED"
  end
end

function map(value, iMin, iMax, oMin, oMax) local n, v
  n = ((value - iMin) / (iMax - iMin))
  v = oMin + n * (oMax-oMin)
  return v
end

function setColor()
    if trackSet == 1 then
      for i = 0, countSelTrack-1 do
        track = reaper.GetSelectedTrack(0, i)
        local r1, g1, b1 = reaper.ColorFromNative(color1)
        local r2, g2, b2 = reaper.ColorFromNative(color2)
        local r = round(map(i, 0, countSelTrack-1, r1, r2))
        local g = round(map(i, 0, countSelTrack-1, g1, g2))
        local b = round(map(i, 0, countSelTrack-1, b1, b2))
        if r and g and b then reaper.SetTrackColor (track, reaper.ColorToNative(r, g, b)) end
      end
    reaper.Undo_EndBlock("Colorize! track", -1)
    elseif itemSet == 1 then
      for i = 0 , countSelItem-1 do
      item = reaper.GetSelectedMediaItem(0, i)
      reaper.SetMediaItemInfo_Value(item, "I_CUSTOMCOLOR", set_color|16777216)
      reaper.UpdateItemInProject(item)
      end
    end
  reaper.TrackList_AdjustWindows(false)
end

function draw_palette(_) local  offset, pal_w
  count = 0
  offset = 2
  pal_w = round(gfx.w - offset)
  for i = 1, col, 1  do
    x = round((i-1) * (pal_w / col) + offset)
    w = round((pal_w /col)- 0.5 * offset)
    h = 20     
    
    for j = 1, row, 1 do
      local red, green, blue, alpha
      if j == 1 then
        red,green,blue=HSLtoRGB(0, 0, 1-i/col) 
      else
        red,green,blue=HSLtoRGB(i/(col), 1, (row-j+1)/row)
      end
        y = round (j * (pal_w / col) + offset)
        gfx.set(red, green, blue, 1)
        gfx.rect (x, y, w, h, 1)
        --border          
        if gfx.mouse_x >= x and gfx.mouse_y >= y and gfx.mouse_x <= x + w and gfx.mouse_y <= y + h then
          for k = 0, 2, 1 do
            gfx.set(0, 0, 0, 0.75 - 0.25 * k)
            gfx.rect (x + k, y + k, w - 2 * k, h - 2 * k, 0)
            local int_r = round(255 * red)
            local int_g = round(255 * green)
            local int_b = round(255 * blue)
            color2 = reaper.ColorToNative(int_r, int_g, int_b) -- integer colour value
            if gfx.mouse_cap == 2 then 
              grad = true
              color1 =  reaper.ColorToNative(int_r, int_g, int_b)
            end
            if grad == false then color1 = color2 end
            setColor()
          end
        else
          gfx.set(0.25, 0.25, 0.25, 0.25)
          gfx.rect (x, y, w, h, 0)
          gfx.set(0, 0, 0, 0.25)
          gfx.rect (x+1, y+1, w-2, h-2, 0) 
        end
    end
  end
end

function window_at_center (title, w, h)
  local l, t, r, b = 0, 0, w, h
  local __, __, screen_w, screen_h = reaper.my_getViewport(l, t, r, b, l, t, r, b, 1)
  local x, y = (screen_w - w) / 2, (screen_h - h) / 2
  gfx.init(title, w, h, 0, x, y)
end

function abort ()
  reaper.Main_OnCommand(40029, 0) -- Undo
  reaper.atexit(exit)
end

function draw()
  mouseContext = reaper.GetCursorContext()
  if mouse_info == nil then mouse_info = 'NOTHING SELECTED' end
  toolbar()
  local width = col * 22 + 2
  local height = (row + 1) * 22 + 2
  window_at_center("Colorize!", width, height)
   
  get_context()
  draw_palette()
  
  gfx.set(0, 1, 1, 0.125)
  gfx.rect(4, 4, gfx.w-8, 16, 1)
  gfx.set(0.25, 1, 1);
  str_w, str_h = gfx.measurestr(mouse_info);
  gfx.x = (gfx.w-str_w)/2;
  gfx.y = 8;
  gfx.drawstr(mouse_info);
  
  local c=gfx.getchar()
  if c == 27 then abort() end
  if gfx.mouse_cap ~= 1 and c~= 27 then reaper.defer(draw) else exit() end -- ESC to exit
  gfx.update()
  
end

function main()
  reaper.Undo_BeginBlock()
  draw()
  reaper.Undo_EndBlock("Colorize! item", -1)
end

main()
That is definitely not my latest commit from 2 days ago.

I have already fix the bug before i even started this thread.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-24-2020, 05:43 PM   #14
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

for comparison

here directly raw copy-pasted from my github

Code:
--[[
 * ReaScript Name: Colorize!
 * About: Track/Item coloring palette instantaneously with minimum click
 * Screenshot: https://stash.reaper.fm/40517/main.png
 * Author: JRENG!
 * Author URI: http://jrengmusic.com
 * Repository: GitHub > jrengmusic > ReaScript
 * Repository URI: https://github.com/jrengmusic/ReaScript
 * Licence: WTFPL
 * Forum Thread: JRENG! Colorize!
 * Forum Thread URI: https://forum.cockos.com/showthread.php?t=243885
 * REAPER: 5.0
 * Version: 1.0
--]]
 
--[[
 * Changelog:
 * v1.0 (2020-10-23)
  # Public release

--]]

grad = false
----------------------- USER CONFIG --------------------
row = 11
col = 48
--------------------------------------------------------

function toolbar ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,1)
  reaper.RefreshToolbar2(sec,cmd)
  reaper.atexit(exit)
end

function exit ()
  isnew,fn,sec,cmd=reaper.get_action_context()
  reaper.GetToggleCommandState(sec,cmd,0)
  reaper.SetToggleCommandState(sec,cmd,0)
  reaper.RefreshToolbar2(sec,cmd)
  gfx.quit()
end

function round(n)
  return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end

function HSLtoRGB(h,s,l)
    local r, g, b
    if s == 0 then
      r, g, b = l, l, l -- achromatic
    else
      function hue2rgb(p, q, t)
          if t < 0 then t = t + 1 end
          if t > 1 then t = t - 1 end
          if t < .166 then return p + (q - p) * 6 * t end
          if t < 0.5 then return q end
          if t < .666 then return p + (q - p) * (.666 - t) * 6 end
          return p
      end
      local q
        if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
        local p = 2 * l - q
        r = hue2rgb(p, q, h + .333)
        g = hue2rgb(p, q, h)
        b = hue2rgb(p, q, h - .333)
    end
    return r, g, b
end

function get_context ()
  countSelTrack=reaper.CountSelectedTracks(0)
  countSelItem=reaper.CountSelectedMediaItems(0)
  if mouseContext==0 and countSelTrack > 0 then
    trackSet=1 itemset=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif mouseContext==1 and countSelItem > 0 then
    itemSet=1 trackSet=0
    mouse_info=countSelItem.." ".."ITEM SELECTED"
  elseif mouseContext==1 and countSelItem == 0 and countSelTrack>0 then
    trackSet=1 itemSet=0
    mouse_info=countSelTrack.." ".."TRACK SELECTED"
  elseif countSelTrack==0 and countSelItem == 0 then
    trackSet=0 itemSet=0
    mouse_info="NOTHING SELECTED"
  end
end

function map(value, iMin, iMax, oMin, oMax) local n, v
  n = ((value - iMin) / (iMax - iMin))
  v = oMin + n * (oMax-oMin)
  return v
end

function setColor()
    if trackSet == 1 then
      for i = 0, countSelTrack-1 do
        track = reaper.GetSelectedTrack(0, i)
        local r1, g1, b1 = reaper.ColorFromNative(color1)
        local r2, g2, b2 = reaper.ColorFromNative(color2)
        local r = round(map(i, 0, countSelTrack-1, r1, r2))
        local g = round(map(i, 0, countSelTrack-1, g1, g2))
        local b = round(map(i, 0, countSelTrack-1, b1, b2))
        if r >= 0 and g >= 0 and b >= 0 then
          local color = reaper.ColorToNative(r, g, b)
          reaper.SetTrackColor (track, color)
        end
      end
    reaper.Undo_EndBlock("Colorize! track", -1)
    elseif itemSet == 1 then
      for i = 0 , countSelItem-1 do
        local r1, g1, b1 = reaper.ColorFromNative(color1)
        local r2, g2, b2 = reaper.ColorFromNative(color2)
        local r = round(map(i, 0, countSelItem-1, r1, r2))
        local g = round(map(i, 0, countSelItem-1, g1, g2))
        local b = round(map(i, 0, countSelItem-1, b1, b2))
        if r >= 0 and g >= 0 and b >= 0 then
          local color = reaper.ColorToNative(r, g, b)
          item = reaper.GetSelectedMediaItem(0, i)
          reaper.SetMediaItemInfo_Value(item, "I_CUSTOMCOLOR", color|16777216)
          reaper.UpdateItemInProject(item)
        end
      end
    end
  reaper.TrackList_AdjustWindows(false)
end

function draw_palette(_) local  offset, pal_w
  count = 0
  offset = 2
  pal_w = round(gfx.w - offset)
  for i = 1, col, 1  do
    x = round((i-1) * (pal_w / col) + offset)
    w = round((pal_w /col)- 0.5 * offset)
    h = 20     
    
    for j = 1, row, 1 do
      local red, green, blue, alpha
      if j == 1 then
        red,green,blue=HSLtoRGB(0, 0, 1-i/col) 
      else
        red,green,blue=HSLtoRGB(i/(col), 1, (row-j+1)/row)
      end
        y = round (j * (pal_w / col) + offset)
        gfx.set(red, green, blue, 1)
        gfx.rect (x, y, w, h, 1)
        --border          
        if gfx.mouse_x >= x and gfx.mouse_y >= y and gfx.mouse_x <= x + w and gfx.mouse_y <= y + h then
          for k = 0, 2, 1 do
            gfx.set(0, 0, 0, 0.75 - 0.25 * k)
            gfx.rect (x + k, y + k, w - 2 * k, h - 2 * k, 0)
            local int_r = round(255 * red)
            local int_g = round(255 * green)
            local int_b = round(255 * blue)
            color2 = reaper.ColorToNative(int_r, int_g, int_b) -- integer colour value
            if gfx.mouse_cap == 2 then 
              grad = true
              color1 =  reaper.ColorToNative(int_r, int_g, int_b)
            end
            if grad == false then color1 = color2 end
            setColor()
          end
        else
          gfx.set(0.25, 0.25, 0.25, 0.25)
          gfx.rect (x, y, w, h, 0)
          gfx.set(0, 0, 0, 0.25)
          gfx.rect (x+1, y+1, w-2, h-2, 0) 
        end
    end
  end
end

function window_at_center (title, w, h)
  local l, t, r, b = 0, 0, w, h
  local __, __, screen_w, screen_h = reaper.my_getViewport(l, t, r, b, l, t, r, b, 1)
  local x, y = (screen_w - w) / 2, (screen_h - h) / 2
  gfx.init(title, w, h, 0, x, y)
end

function abort ()
  reaper.Main_OnCommand(40029, 0) -- Undo
  reaper.atexit(exit)
end

function draw()
  mouseContext = reaper.GetCursorContext()
  if mouse_info == nil then mouse_info = 'NOTHING SELECTED' end
  toolbar()
  local width = col * 22 + 2
  local height = (row + 1) * 22 + 2
  window_at_center("Colorize!", width, height)
   
  get_context()
  draw_palette()
  
  gfx.set(0, 1, 1, 0.125)
  gfx.rect(4, 4, gfx.w-8, 16, 1)
  gfx.set(0.25, 1, 1);
  str_w, str_h = gfx.measurestr(mouse_info);
  gfx.x = (gfx.w-str_w)/2;
  gfx.y = 8;
  gfx.drawstr(mouse_info);
  
  local c=gfx.getchar()
  if c == 27 then abort() end
  if gfx.mouse_cap ~= 1 and c~= 27 then reaper.defer(draw) else exit() end -- ESC to exit
  gfx.update()
  
end

function main()
  reaper.Undo_BeginBlock()
  draw()
  reaper.Undo_EndBlock("Colorize! item", -1)
end

main()
I don't why ReaPack gives you all the buggy one and not the latest commit. I also have rebuild the index before i push it to github.

Aaah man this ReaPack gives me a lot of headache.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-24-2020, 07:30 PM   #15
sonictim
Human being with feelings
 
sonictim's Avatar
 
Join Date: Feb 2020
Location: Los Angeles
Posts: 463
Default

2 suggestions... if you change code, but do not change your version number... reapack will not update to match your repo....

unless you do reapack-index. -a. (amend). which I believe will look for updates to the current version of scripts and update the code.

still, I don't trust it....

so I think it's best to just update your version to be safe....

even then, I still have trouble with it... it doesn't always work...

Even today, I had trouble... it kept looking at an old version of a header I had in my git that I had long since deleted... but for some reason, reapack kept referring to it and ignoring my current version.
sonictim is offline   Reply With Quote
Old 10-24-2020, 07:37 PM   #16
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Thanks for clarifying sonictim.

This github and reapack thingy isn't just my thing hahahaha

Does the latest commit of the script works for you?
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-25-2020, 07:05 AM   #17
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

Previously, there's a problem with the way i create index for ReaPack–thus the one that ReaPack sync is not the latest commit because i didn't change the version number.

Anyhow, the problem has been sorted out. Now if you download via ReaPack, it will download the latest commit without item selection bug. The version number is still the same v1.0 (public release).

If you already installed the script previously, please uninstall it first. Perhaps to remove and re-add my repo. And install the script again.

Thanks
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Old 10-25-2020, 07:20 AM   #18
bFooz
Human being with feelings
 
Join Date: Jul 2010
Location: Slovakia
Posts: 2,588
Default

I did reinstalled your repository and now it seems to work ok.

Another ideas:
- when mouse is hovered out of the script window, the track color should change to the original one.
- when righclicked to select a starting color of a gradient, that swatch could be highlighted

Last edited by bFooz; 10-25-2020 at 07:27 AM.
bFooz is online now   Reply With Quote
Old 10-25-2020, 01:58 PM   #19
sonictim
Human being with feelings
 
sonictim's Avatar
 
Join Date: Feb 2020
Location: Los Angeles
Posts: 463
Default

awesome! working now!
sonictim is offline   Reply With Quote
Old 10-25-2020, 02:06 PM   #20
sonictim
Human being with feelings
 
sonictim's Avatar
 
Join Date: Feb 2020
Location: Los Angeles
Posts: 463
Default

one more idea for you.... perhaps you could create a place to "set to default color" in the GUI
sonictim is offline   Reply With Quote
Old 11-15-2020, 04:15 PM   #21
DarrenH
Human being with feelings
 
Join Date: Mar 2014
Posts: 347
Default

Love this! So simple...thanks for your work.

The only feedback I have (Win10) is that the X button doesn't work...it just opens the window again.

Thanks again
DarrenH is offline   Reply With Quote
Old 11-15-2020, 04:41 PM   #22
jrengmusic
Human being with feelings
 
jrengmusic's Avatar
 
Join Date: Jun 2015
Location: Indonesia Raya
Posts: 684
Default

It was designed to be closed only after you choose a color or cancel by pressing ESC key.
__________________
JRENG! | M E T R I C
jrengmusic is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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

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

Forum Jump


All times are GMT -7. The time now is 03:19 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.