z_mirror/PipeVisualizer
git clone https://git.raiguard.me/z_mirror/PipeVisualizer
32cb6c3
main
1--- Converts an RGB color value to HSV. Conversion formula 2--- adapted from http://en.wikipedia.org/wiki/HSV_color_space. 3--- @param input Color 4--- @return number, number, number, number 5local function rgb_to_hsv ( input ) 6local r =input . r --[[@as number]] 7local g =input . g --[[@as number]] 8local b =input . b --[[@as number]] 9local a =input . a --[[@as number]] 10local max , min =math . max ( r , g , b ), math . min ( r , g , b ) 11local h , s , v 12v =max 13 14local d =max - min 15if max == 0 then 16s =0 17else 18s =d / max 19end 20 21if max == min then 22h =0 -- achromatic 23else 24if max == r then 25h =( g - b ) / d 26if g < b then 27h =h + 6 28end 29elseif max == g then 30h =( b - r ) / d + 2 31elseif max == b then 32h =( r - g ) / d + 4 33end 34h =h / 6 35end 36 37return h , s , v , a 38end 39 40--- Converts an HSV color value to RGB. Conversion formula 41--- adapted from http://en.wikipedia.org/wiki/HSV_color_space. 42--- @param h number 43--- @param s number 44--- @param v number 45--- @param a number 46--- @return Color 47local function hsv_to_rgb ( h , s , v , a ) 48local r , g , b 49 50local i =math . floor ( h * 6 ) 51local f =h * 6 - i 52local p =v * ( 1 - s ) 53local q =v * ( 1 - f * s ) 54local t =v * ( 1 - ( 1 - f ) * s ) 55 56i =i % 6 57 58if i == 0 then 59r , g , b =v , t , p 60elseif i == 1 then 61r , g , b =q , v , p 62elseif i == 2 then 63r , g , b =p , v , t 64elseif i == 3 then 65r , g , b =p , q , v 66elseif i == 4 then 67r , g , b =t , p , v 68elseif i == 5 then 69r , g , b =v , p , q 70end 71 72return { r =r , g =g , b =b , a =a } 73end 74 75--- @param e EventData.CustomInputEvent 76local function on_color_by_system_changed ( e ) 77global . color_by_system [ e . player_index ] =not global . color_by_system [ e . player_index ] 78local player =game . get_player ( e . player_index ) --[[@as LuaPlayer]] 79player . create_local_flying_text ( { 80text ={ global . color_by_system [ e . player_index ] and "message.pv-color-by-system" or "message.pv-color-by-fluid" } , 81create_at_cursor =true , 82} ) 83end 84 85local function generate_system_colors () 86--- @type Color[] 87global . system_colors ={} 88for s =0.8 , 0.3 , - 0.1 do 89for h =0 , 360 , 30 do 90global . system_colors [ # global . system_colors + 1 ] =hsv_to_rgb ( h / 360 , s , 1 , 1 ) 91end 92end 93end 94 95local function generate_fluid_colors () 96--- @type table<string, Color> 97local colors ={} 98for name , fluid in pairs ( game . fluid_prototypes ) do 99local h , s , v , a =rgb_to_hsv ( fluid . base_color ) 100v =1 101colors [ name ] =hsv_to_rgb ( h , s , v , a ) 102end 103global . fluid_colors =colors 104end 105 106local function initialize () 107global . color_by_system ={} 108generate_fluid_colors () 109generate_system_colors () 110end 111 112local colors ={} 113 114colors . on_init =initialize 115colors . on_configuration_changed =initialize 116 117colors . events ={ 118[ "pv-color-by-fluid-system" ] =on_color_by_system_changed , 119} 120 121return colors