z_mirror/PipeVisualizer
git clone https://git.raiguard.me/z_mirror/PipeVisualizer
0c95e61
2.0
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 75local function generate_fluid_colors () 76--- @type table<string, Color> 77local colors ={} 78for name , fluid in pairs ( game . fluid_prototypes ) do 79local h , s , v , a =rgb_to_hsv ( fluid . base_color ) 80v =1 81colors [ name ] =hsv_to_rgb ( h , s , v , a ) 82end 83global . fluid_colors =colors 84end 85 86local colors ={} 87 88colors . on_init =generate_fluid_colors 89colors . on_configuration_changed =generate_fluid_colors 90 91return colors