--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library is distributed in the hope that it will be useful,            --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of             --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser General Public           --
-- License along with this library; if not, write to the Free Software        --
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with RASCAL.OS;
with RASCAL.Utility;    use RASCAL.Utility;

with Kernel;            use Kernel;
with Interfaces.C;      use Interfaces.C;


package body RASCAL.Colour is

   Wimp_ReadPalette               : constant := 16#400E5#;
   ColourTrans_ReturnColourNumber : constant := 16#40744#;

   --
 
   procedure Read_Palette (Palette     : out Palette_Type;
                           True_Colour : in Boolean := false) is

      Register : aliased Kernel.swi_regs;
      Error    : oserror_access;
   begin
      Register.R(1) := Adr_To_Int(Palette'Address);
      if True_Colour then
         Register.R(2) := 16#45555254#;
      else
         Register.R(2) := 0;
      end if;
      Error := Kernel.swi(Wimp_ReadPalette,register'Access,register'Access);

      if Error /=null then
         pragma Debug(Report("Colour.Read_Palette: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);         
      end if;
   end Read_Palette;

   --

   function Get_Inverted (Colour : in Wimp_Colour) return Palette_Colour is

      Palette          : Palette_Type;
      Background,Total : Integer;
   begin
      Read_Palette (Palette,True_Colour => false);
      Background := Integer(Palette(Palette_Entries_Type'Val(Integer(Colour))));
      Total := ("and"(Background / 256,16#FF#)*2) +
               ("and"(Background / 65536,16#FF#)*4) +
               "and"(Background / 16777216,16#FF#);
      if Total < 765 then
         return 0;
      end if;
      return 7;         
   end Get_Inverted;

   --
      
end RASCAL.Colour;
