function counts = count_colours ( m, n, C )

%*****************************************************************************80
%
%% count_colours() counts the squares of each color in a C-coloured rectangle.
%
%  Discussion: 
%
%    We use the formula derived by Fabio Visonà on StackExchange 
%    and rigorously justified in our paper.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    16 February 2023
%
%  Author:
%
%    Marcus Garvie
%
%  Input:
%
%    integer m, n: the dimensions of the rectangle.
%
%    integer C: the number of colours.
%
%  Output:
%
%    integer counts(c): the number of squares of each colour.
%  

%
%  Allocate the output array.
%
  counts = zeros ( 1, C );
%
%  Calculate some quantities.
%
  a = mod ( m, C );
  b = mod ( n, C );
  N = ( m * n - a * b ) / C;

  for x = 1 : C
    
    if ( x <= a + b - 1 - C )
      counts(x) = N + a + b - C;
    elseif ( a + b - 1 - C < x ) && ( x < min ( a, b ) )
      counts(x) = N + x;
    elseif ( min ( a, b ) <= x ) && ( x <= max ( a, b ) )
      counts(x) = N + min(a,b);
    elseif ( max ( a, b ) < x ) && ( x <= a + b - 1 )
      counts(x) = N + a +b - x;
    else
      counts(x) = N;
    end

  end

  return
end
