/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/************************************************************************/
/* � Acorn Computers Ltd, 1992.                                         */
/*                                                                      */
/* This file forms part of an unsupported source release of RISC_OSLib. */
/*                                                                      */
/* It may be freely used to create executable images for saleable       */
/* products but cannot be sold in source form or as an object library   */
/* without the prior written consent of Acorn Computers Ltd.            */
/*                                                                      */
/* If this file is re-distributed (even if modified) it should retain   */
/* this copyright notice.                                               */
/*                                                                      */
/************************************************************************/

/*
 * Title: colourmenu.c
 * Purpose: create a wimp colour setting menu
 * History: IDJ: 05-Feb-92: prepared for source release
 *
 */

#define BOOL int
#define TRUE 1
#define FALSE 0

#include <stdio.h>

#include "os.h"
#include "wimp.h"
#include "menu.h"
#include "colourmenu.h"

/* Macro - form weighted sum of colours. From is an index into the palette */
#define ColourSum(from) (palette.c[from].bytes.red + \
                  palette.c[from].bytes.green + palette.c[from].bytes.blue)

/* Creates a menu containing each of the 16 wimp colours, and optionally a
   'None' entry, and returns a handle to it. The text in the colour is plotted
   in black or white, depending on the background
*/

menu colourmenu_make(char *title, BOOL includeNone)
{
  menu m;
  wimp_menuhdr  *dptr;
  wimp_menuitem *iptr;                  /* Very mode dependent */
  wimp_palettestr palette;
  int i, white, black, threshold;

  /* Create the menu */
  if (includeNone)
    m = menu_new(title,
 "  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,  11,  12,  13,  14,  15,None"
              );
  else
    m = menu_new(title,
 "  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,  11,  12,  13,  14,  15"
              );

  /* See whether colour 0 or colour 7 is white to allow for inverted palette */
  wimp_readpalette(&palette);
  if (ColourSum(0) > ColourSum(7))
  {
    white = 0;
    black = 7;
  }
  else
  {
    white = 7;
    black = 0;
  }

  /* Set threshold to half the white intensity */
  threshold = ColourSum(white) / 2;
  black *= wimp_IFORECOL;
  white *= wimp_IFORECOL;

  /* Check each colour against the threshold */
  for (dptr = menu_syshandle(m), i = 0; i <= 15; ++i)
  {
    iptr = ((wimp_menuitem *)(dptr + 1) + i);
    iptr->iconflags = (iptr->iconflags & ~(wimp_IFORECOL * 15))
                       | (i * wimp_IBACKCOL)
                       | ((ColourSum(i) > threshold) ? black : white);
  }

  return(m);
}

/* end */