--- lib/color.ml 2023-03-21 18:21:51.000000000 +0800 +++ lib/color.ml 2025-12-28 10:59:20.000000000 +0800 @@ -9,26 +9,53 @@ let pp ppf t = Format.fprintf ppf "%#lx" t (** [to_int8s color] returns [(red, green, blue, alpha)] components of color.*) -let to_int8s n32 = - let n = Int32.to_int n32 in - let r = (n lsr 24) land 255 in - let g = (n lsr 16) land 255 in - let b = (n lsr 8) land 255 in - let a = n land 255 in - (r,g,b,a) +let to_int8s = + match Sys.big_endian with + | false -> + (fun n32 -> + let n = Int32.to_int n32 in + let r = (n lsr 24) land 255 in + let g = (n lsr 16) land 255 in + let b = (n lsr 8) land 255 in + let a = n land 255 in + (r,g,b,a) + ) +| true -> + (fun n32 -> + let n = Int32.to_int n32 in + let a = (n lsr 24) land 255 in + let b = (n lsr 16) land 255 in + let g = (n lsr 8) land 255 in + let r = n land 255 in + (r,g,b,a) + ) (** [of_rgba r g b a] returns a color from the given components.*) let of_rgba = let f n = Int32.of_int (max 0 (min n 255)) in - fun r g b a -> - Int32.( - logor (shift_left (f r) 24) - (logor (shift_left (f g) 16) - (logor (shift_left (f b) 8) - (f a) - ) - ) - ) + match Sys.big_endian with + | false -> + (fun r g b a -> + Int32.( + logor (shift_left (f r) 24) + (logor (shift_left (f g) 16) + (logor (shift_left (f b) 8) + (f a) + ) + ) + ) + ) + | true -> + (fun r g b a -> + Int32.( + logor (shift_left (f a) 24) + (logor (shift_left (f b) 16) + (logor (shift_left (f g) 8) + (f r) + ) + ) + ) + ) (** [of_rgba_0_1 r g b a] returns a color from the given components as floats. Each component must be between [0.] and [1.] and is multiplicated by 255 to