Results 1 to 3 of 3

Thread: UpdateColorFromArray

  1. #1
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default UpdateColorFromArray

    Its back, better than last time

    I wanted to try and do it some other than the classic

    "If Abs(HSL1 - HSL2) < Dif[x] Then..." from Hue to Lum


    Fixed:

    SCAR Code:
    program new;

    Function Mean(Values: TExtendedArray): Extended;
    Var
      I: Integer;
    Begin
      For I := 0 To High(Values) Do
        Result := Result + Values[i];
      Result := Result / GetArrayLength(Values);
    End;


    Function UpdateColorFromArray(Ref: Integer; Colors: TIntegerArray): Integer;
    Var
      Dif: Array of Array [0..2] Of Extended;
      HSL, HSL2: Array [0..2] Of Extended;
      SortedDif: TExtendedArray;
      C, I, L, D: Integer;
      R: Extended;
    Begin
      ColorToHSL(Ref, HSL[0], HSL[1], HSL[2]);
      SetArrayLength(Dif, GetArrayLength(Colors));
      L := High(Colors);
      For I := 0 To L Do
      Begin
        ColorToHSL(Colors[i], HSL2[0], HSL2[1], HSL2[2]);
        For C := 0 To 2 Do
          Dif[i][c] := Abs(HSL[c] - HSL2[c]);
      End;
      L := High(Dif);
      SetArrayLength(SortedDif, L + 1);
      For D := 0 To L Do
        SortedDif[d] := Mean([Dif[d][0], Dif[d][1], Dif[d][2]]);
      R := AMinE(SortedDif);
      For C := 0 To L Do
        If SortedDif[c] = R Then
        Begin
          Result := Colors[c];
          Exit;
        End;
    End;

    begin
      Writeln(IntToStr(UpdateColorFromArray(255, [210, 22, 244])));
    End.


    If the results don't meet what you want, change HSL to RGB or some other format...



    Ehm - what it does

    It picks the color closest to ref from the array you fill colors in - Colors.

    It's useful for stuff like for example if you have a script, that "learns as it goes" (...like tarajunky?), you can then do a quick UpdateColorFromArray to nill your color array of all the past colors, and start all over again with a new fresh color

  2. #2
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    I guess it's not working as designed? It always returns the first one in the array for me.

    Writeln(IntToStr(UpdateColorFromArray(clBlack, [243, 242, 255])));
    Writeln(IntToStr(UpdateColorFromArray(clRed, [243, 242, 255])));
    Writeln(IntToStr(UpdateColorFromArray(clBlack, [255, 243, 242])));
    Writeln(IntToStr(UpdateColorFromArray(clRed, [242, 242, 255])));
    Hup Holland Hup!

  3. #3
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Aww crap, I forgot about that since the other version had another kind of bug -.-

    Fixing..

    Done

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •