Advertisement
poulhoi

phoi_Color tracks by place in folder hierarchy.lua

Dec 29th, 2020 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. -- USER VARIABLES
  2. colorOffset = -30
  3. -- the offset to apply to the color of each track for each generation down the hierarchy.
  4. --  Set to negative colors to make children darker than parents and vice versa.
  5.  
  6.  
  7. -- NAME
  8. local scriptName = ({reaper. get_action_context()})[2]:match("([^/\\]+)%.lua$") -- generate default scriptName from file
  9.  
  10. --FUNCTIONS FOR DEBUG
  11. function msg(msg)
  12.   reaper.ShowConsoleMsg(tostring(msg) .. "\n")
  13. end
  14.  
  15. --- FUNCTIONS
  16.  
  17. function getSelectedTracks()
  18.   local tracks = {}
  19.   local tCount = reaper.CountSelectedTracks(0)
  20.   for i = 1, tCount do
  21.     tracks[i] = reaper.GetSelectedTrack(0, i - 1)
  22.   end
  23.   return tracks
  24. end
  25.  
  26. function getTrackLevel(track) -- get how many "levels" down the hiearchy the track is; 0 for parents, 1 for first-generation children etc.
  27.   local level = 0
  28.   local found = false
  29.   local cur = track
  30.  
  31.   while not found do
  32.     local up = reaper.GetParentTrack(cur)
  33.     if up == nil then -- if parent doesn't exist
  34.       found = true
  35.       return level
  36.     else
  37.       level = level + 1
  38.       cur = up
  39.     end
  40.   end
  41.  
  42. end
  43.  
  44. function offsetTrackColor(track, offset)
  45.   local prevColorNative = reaper.GetTrackColor(track)
  46.   local prevColorR, prevColorG, prevColorB = reaper.ColorFromNative(prevColorNative)
  47.   local newColorR = math.max(math.min(prevColorR + offset, 255), 0)
  48.   local newColorG = math.max(math.min(prevColorG + offset, 255), 0)
  49.   local newColorB = math.max(math.min(prevColorB + offset, 255), 0)
  50.   local newColorNative = reaper.ColorToNative(newColorR, newColorG, newColorB)
  51.   reaper.SetTrackColor(track, newColorNative)
  52. end
  53. ----- END OF FUNCTIONS
  54.  
  55.  
  56. function main()
  57.   reaper.Undo_BeginBlock()
  58.   for i = 0, reaper.CountSelectedTracks(0) - 1 do
  59.     local tr = reaper.GetSelectedTrack(0, i)
  60.     local trLvl = getTrackLevel(tr)
  61.     offsetTrackColor(tr, trLvl * colorOffset)
  62.   end
  63.  
  64.   reaper.Undo_EndBlock(scriptName, -1)
  65. end
  66.  
  67. reaper.PreventUIRefresh(1)
  68. main()
  69. reaper.PreventUIRefresh(-1)
  70. reaper.UpdateArrange()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement