realbasic-games
[Top] [All Lists]

Re: Selecting Colors

To: cmalumphy at earthlink dot net, REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: Selecting Colors
From: Jonathan Johnson <jonj at realsoftware dot com>
Date: Sun, 30 May 2004 08:19:51 -0500
Cc:
Delivered-to: realbasic-games at lists dot realsoftware dot com
List-help: <mailto:realbasic-games-request@lists.realsoftware.com?subject=help>
List-id: REALbasic Games <realbasic-games.lists.realsoftware.com>
List-post: <mailto:realbasic-games@lists.realsoftware.com>
References: <40B9DA07 dot 34617579 at earthlink dot net>

On May 30, 2004, at 7:56 AM, Chris Malumphy wrote:

I'm making a game where I need to randomly select 5 discernably
different colors for each round of play. It would be nice if the range
of colors was as wide as possible, although it would also be nice if the
combination of the 5 selected colors was at least somewhat attractive.
The game will randomly fill small hexagons or rectangles with these
colors.

Here's something I just whipped up:

  dim c() as Color
  dim i,j as integer
  dim newColor as Color
  dim rand as new Random
  dim isUnique as Boolean

  CONST NumColors = 5
  CONST Tolerance = .10

  for i = 0 to NumColors - 1
    while true
newColor = rgb(rand.InRange(0,255),rand.InRange(0,255),rand.InRange(0,255))

      isUnique = true

      for j = 0 to ubound(c)
        if abs(newColor.hue - c(j).hue) < Tolerance or _
            abs(newColor.saturation - c(j).saturation) < Tolerance or _
            abs(newColor.value - c(j).value) < Tolerance then
          isUnique = false
        end if
      next

      if isUnique then
        c.Append newColor
        exit
      end if
    wend
  next

You can tweak Tolerance a bit, but .1 seems pretty good. I'm comparing HSV values because what really distinguishes a color isn't the amount of Red that changed, but rather if the Hue, Saturation, or Value changed.

However, this code does not notice if colors are clashing colors. I'm not even sure how to approach that problem ;)

Good luck and HTH,
Jon


--
Jonathan Johnson
Testing Department
REAL Software, Inc.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://support.realsoftware.com/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>