demo1[bct`Private`file_:"./utensils.ppm"] := 
  Module[{}, RunThrough[StringJoin["export LD_LIBRARY_PATH; ", "LD_LIBRARY_\
       PATH=/util/X11/lib:/usr/openwin/lib:/usr/lib:/util/lib; ", "cat ",
       bct`Private`file, " | ./ColorNaming &"], Null]; Pause[5]; 
    Print["*** Adaptation, theta=0.35, RgbToAbl ***"]; bct`Private`FindBW[]; 
    Print["*** Color Naming, simple names ***"]; 
    bct`Private`NameSamples[False, 1, 0.35, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Color Naming, complex names ***"]; 
    bct`Private`NameSamples[True, 1, 0.35, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Pointing Out Colors, simple or\
      complex names ***"]; bct`Private`PointOutSamples[False, 3, 0.01, 
     bct`Private`BccEnglishAblCat6, bct`Private`RgbToAbl]; 
    Print["*** Choosing Objects by Color, simple names ***"]; 
    bct`Private`SelectObject[bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Null]
 
bct`Private`FindBW[bct`Private`subSampledFile_:"./.subsampled.ppm",
  bct`Private`subSampledSizeFile_:"./.subsampled.xy"] := 
  Module[{bct`Private`mtime}, bct`Private`mtime = 
     FileDate[bct`Private`subSampledFile]; Print["Please sample the image"]; 
    While[FileDate[bct`Private`subSampledFile] == bct`Private`mtime, 
     Pause[0.5]]; Print["working..."]; Pause[1]; 
    bct`Private`FindBW1[ReadPpmFile[bct`Private`subSampledFile], 
     bct`Private`GetOriginalDimensions[bct`Private`subSampledSizeFile]]; 
    Print[StringForm["{BlackRGB, WhiteRGB} = ``", 
      {bct`Private`BlackRGB, bct`Private`WhiteRGB}]]]
 
bct`Private`FindBW1[bct`Private`samples_, bct`Private`origDims_] := 
  Module[{bct`Private`sam, s, bct`Private`minmax, bct`Private`bw, 
    eta = 0.01}, bct`Private`sam = Flatten[bct`Private`samples, 1]; 
    s = Select[bct`Private`sam, 
      Apply[Plus, ((#1[[1]] - #1[[2]])^2 & ) /@ 
          Partition[(Append[#1, #1[[1]]] & )[#1], 2, 1]] < eta & ]; 
    s = Transpose[{s, (Apply[Plus, #1] & ) /@ s}]; 
    bct`Private`minmax = ({Min[#1], Max[#1]} & )[(#1[[2]] & ) /@ s]; 
    bct`Private`bw = 
     (Select[s, Function[bct`Private`x, bct`Private`x[[2]] == #1], 1] & ) /@ 
      bct`Private`minmax; {bct`Private`BlackRGB, bct`Private`WhiteRGB} = 
     {First[bct`Private`bw[[1]]][[1]], Last[bct`Private`bw[[2]]][[1]]}; 
    Removed["WriteIndexFile"][{{bct`Private`BlackRGB, 0, 
       Position[bct`Private`sam, bct`Private`BlackRGB][[1,1]]}, 
      {bct`Private`WhiteRGB, 0, 
       Position[bct`Private`sam, bct`Private`WhiteRGB][[1,1]]}}, 
     Dimensions[bct`Private`samples], bct`Private`origDims]; 
    {bct`Private`BlackRGB, bct`Private`WhiteRGB}]
 
bct`Private`BlackRGB = {0, 0, 0}
 
bct`Private`WhiteRGB = {1, 1, 1}
 
ReadPpmFile[mygraphics`Private`filename_:"./.sample.ppm"] := 
  Module[{mygraphics`Private`f, mygraphics`Private`type, 
    mygraphics`Private`width, mygraphics`Private`height, 
    mygraphics`Private`line, mygraphics`Private`s, 
    mygraphics`Private`maxval, mygraphics`Private`data}, 
   mygraphics`Private`f = OpenRead[mygraphics`Private`filename]; 
    If[mygraphics`Private`f == $Failed, Return[]]; 
    mygraphics`Private`type = Read[mygraphics`Private`f, String]; 
    If[mygraphics`Private`type == EndOfFile, 
     Close[mygraphics`Private`f]; Return[{}]]; 
    If[mygraphics`Private`type != "P3" && mygraphics`Private`type != "P6", 
     Print[StringForm["Unknown file type `` for file ``", 
        mygraphics`Private`type, mygraphics`Private`filename]]; 
      Close[mygraphics`Private`f]; Return[]]; 
    While[StringMatchQ[mygraphics`Private`line = 
       Read[mygraphics`Private`f, String], "#*"]]; 
    mygraphics`Private`s = StringToStream[mygraphics`Private`line]; 
    {mygraphics`Private`width, mygraphics`Private`height} = 
     Read[mygraphics`Private`s, {Number, Number}]; 
    Close[mygraphics`Private`s]; 
    mygraphics`Private`maxval = Read[mygraphics`Private`f, Number]; 
    Read[mygraphics`Private`f, Character]; 
    If[mygraphics`Private`type == "P3", 
     mygraphics`Private`data = 
      Read[mygraphics`Private`f, 
       Table[Table[{Number, Number, Number}, {mygraphics`Private`width}], 
        {mygraphics`Private`height}]], 
     mygraphics`Private`data = 
      Read[mygraphics`Private`f, 
       Table[Table[{Byte, Byte, Byte}, {mygraphics`Private`width}], 
        {mygraphics`Private`height}]]]; Close[mygraphics`Private`f]; 
    N[mygraphics`Private`data/mygraphics`Private`maxval]]
 
bct`Private`GetOriginalDimensions[bct`Private`filename_:"./.subsampled.xy"] := 
  Module[{bct`Private`f, dims}, 
   bct`Private`f = OpenRead[bct`Private`filename]; 
    dims = Read[bct`Private`f, Table[Number, {6}]]; Close[bct`Private`f]; dims
    ]
 
bct`Private`NameSamples[bct`Private`complexNames_:False, num_:1, theta_:1/2, 
   bct`Private`categories_:
    {{{{0, 0, 10.11758111488888}, 8.59301680280366, 
       {1., 1., 0.999999999707514}, {"Bk", "black", {0, 0, 0}}, 
       0.00006706993659198796}, 
      {{0, 0, 99.1381861709482}, 8.33652823891848, 
       {1., 1., 0.999999999989715}, {"W", "white", {1, 1, 1}}, 
       0.1165768032502432}, {{0, 0, 55.41087739347514}, 21.3036871078195, 
       {1., 1., 1.000006840564639}, {"Gy", "gray", {0.85, 0.85, 0.85}}, 
       0.002415247101687503}}, 
     {{{70.47570603430913, 51.86457391959739, 29.162903296745}, 
       29.68388136790197, {1.110168089319956, 1.229103130012056, 
        1.605296816664047}, {"R", "red", {1, 0, 0}}, 0.1056261712449114}, 
      {{-131.8203292392433, 110.5253109936243, -42.31441492755766}, 
       121.4068101850715, {0.92595478850094, 0.990606158429391, 
        1.036764880056181}, {"G", "green", {0, 1, 0}}, 0.05635215606863495}\
       , {{10.13901422726245, 145.5863830742936, 32.00422215326372}, 
       48.36929902392832, {0.99999997235607, 0.999999394430672, 
        0.999999363999513}, {"Y", "yellow", {1, 1, 0}}, 0.0941468914421895}\
       , {{-46.13065222533805, -95.1451579123497, 62.98942951278652}, 
       71.97467040860237, {1.001328371345833, 0.998392114075059, 
        1.000146909152531}, {"B", "blue", {0, 0, 1}}, 0.07133269965963059}, 
      {{32.75157702275883, 59.42631853985989, -12.55584806927745}, 
       46.02264175636888, {0.957123479231195, 0.927615418279127, 
        1.03830268256839}, {"Bn", "brown", {11/17, 14/85, 14/85}}, 
       0.0807578947093132}, {{155.4843723215934, -42.08424969329905, 
        23.53141572043431}, 64.96697323168849, 
       {0.999999999983169, 1.000000000000024, 0.999999999998758}, 
       {"Pl", "purple", {32/51, 32/255, 16/17}}, 0.1196564336401132}, 
      {{83.7712989081118, 1.040827997089359, 124.7172473269688}, 
       57.16828391984896, {0.984533159612614, 1.591777215063118, 
        0.924668144895979}, {"Pk", "pink", {1, 64/85, 203/255}}, 
       0.06077940640323634}, {{69.91455255177499, 102.4758032285154, 
        47.23362527276361}, 33.24243730153221, {1., 1., 1.}, 
       {"Or", "orange", {1, 11/17, 0}}, 0.106912407794282}}}, 
   bct`Private`transform_:bct`Private`RgbToAbl, 
   bct`Private`filename_:"./.sample.ppm", 
   bct`Private`thetaFudge_:0.99] := 
  Module[{bct`Private`mtime, bct`Private`sample, r, 
    bct`Private`results = {}}, While[True, 
    Print["sample?"]; bct`Private`mtime = FileDate[bct`Private`filename]; 
     While[FileDate[bct`Private`filename] == bct`Private`mtime, Pause[0.25]]; 
     Pause[0.25]; bct`Private`sample = ReadPpmFile[bct`Private`filename]; 
     If[bct`Private`sample === {}, Return[bct`Private`results]]; 
     If[bct`Private`complexNames, 
      r = ComplexNameExample[bct`Private`transform[bct`Private`sample[[1,
          1]]], bct`Private`categories, theta, bct`Private`thetaFudge], 
      r = NameExample[bct`Private`transform[bct`Private`sample[[1,1]]], 
        bct`Private`categories, num, theta, bct`Private`thetaFudge]]; 
     bct`Private`results = 
      Append[bct`Private`results, {bct`Private`sample[[1,1]], r}]; Print[r]]]
 
bct`Private`RgbToAbl[p_] := 
  XyzToAbl[{RgbCRTToXyz . bct`Private`RGBStretch[p], RgbCRTToXyz . {1, 1, 1}}]
 
XyzToAbl[{{transforms`Private`x_, transforms`Private`y_, 
     transforms`Private`z_}, {transforms`Private`xI_, 
     transforms`Private`yI_, transforms`Private`zI_}}] := 
  Module[{transforms`Private`ls, transforms`Private`as, 
    transforms`Private`bs}, transforms`Private`ls = 
     If[transforms`Private`y/transforms`Private`yI > 0.008856, 
      116*(transforms`Private`y/transforms`Private`yI)^(1/3) - 16, 
      (903.3*transforms`Private`y)/transforms`Private`yI]; 
    transforms`Private`as = 
     500*(If[transforms`Private`x/transforms`Private`xI > 0.008856, 
         (transforms`Private`x/transforms`Private`xI)^(1/3), 
         (7.787*transforms`Private`x)/transforms`Private`xI + 16/116] - 
        If[transforms`Private`y/transforms`Private`yI > 0.008856, 
         (transforms`Private`y/transforms`Private`yI)^(1/3), 
         (7.787*transforms`Private`y)/transforms`Private`yI + 16/116]); 
    transforms`Private`bs = 
     200*(If[transforms`Private`y/transforms`Private`yI > 0.008856, 
         (transforms`Private`y/transforms`Private`yI)^(1/3), 
         (7.787*transforms`Private`y)/transforms`Private`yI + 16/116] - 
        If[transforms`Private`z/transforms`Private`zI > 0.008856, 
         (transforms`Private`z/transforms`Private`zI)^(1/3), 
         (7.787*transforms`Private`z)/transforms`Private`zI + 16/116]); 
    {transforms`Private`as, transforms`Private`bs, transforms`Private`ls}]
 
transforms`Private`x = ArcCos[2.21237085294911/6^(1/2)]
 
RgbCRTToXyz = {{1.049659484292496, 0.807477298143475, 0.4152096720309054}, 
   {0.5515726589435092, 1.671619669840879, 0.1653124108948528}, 
   {0.07020015659281034, 0.3541567097120506, 2.17468476532179}}
 
bct`Private`RGBStretch[p_] := 
  (p - bct`Private`BlackRGB)/(bct`Private`WhiteRGB - bct`Private`BlackRGB)
 
ComplexNameExample[stimulus_, categories_, theta_:1/2, thetaFudge_:0.99] := 
  Module[{names}, names = 
     NameExample[stimulus, categories, 2, 0.75*thetaFudge*theta]; 
    Which[names === {} || names[[1,2]] < thetaFudge*theta, "none", 
     Length[names] === 1, names[[1]], names[[2,2]] > thetaFudge*theta, 
     {StringJoin[names[[2,1]], "-ish ", names[[1,1]]], 
      {names[[2,2]], names[[1,2]]}}, True, 
     {StringJoin["somewhat ", names[[2,1]], "-ish ", names[[1,1]]], 
      {names[[2,2]], names[[1,2]]}}]]
 
NameExample[stimulus_, categories_, num_:1, theta_:1/2, thetaFudge_:0.99] := 
  Module[{match}, match = 
     Select[Flatten[categories, 1], 
      NGaussian[#1[[3]]*stimulus, #1[[3]]*#1[[1]], #1[[2]]] >= 
        thetaFudge*theta & ]; match = 
     ({#1[[4,2]], NGaussian[#1[[3]]*stimulus, #1[[3]]*#1[[1]], 
          #1[[2]]]} & ) /@ match; 
    Take[Sort[match, Function[{m1, m2}, m1[[2]] > m2[[2]]]], 
     If[num > Length[match], Length[match], num]]]
 
NGaussian[p_, mu_, sigma_] := E^(-((EuclideanDistance[p, mu]/sigma)^2/2))
 
EuclideanDistance[p1_, p2_] := Sqrt[Apply[Plus, (p1 - p2)^2]]
 
bct`Private`BccEnglishAblCat6 = 
  {{{{0, 0, 10.11758111488888}, 8.59301680280366, 
     {1., 1., 0.999999999707514}, {"Bk", "black", {0, 0, 0}}, 
     0.00006706993659198796}, {{0, 0, 99.1381861709482}, 8.33652823891848, 
     {1., 1., 0.999999999989715}, {"W", "white", {1, 1, 1}}, 
     0.1165768032502432}, {{0, 0, 55.41087739347514}, 21.3036871078195, 
     {1., 1., 1.000006840564639}, {"Gy", "gray", {0.85, 0.85, 0.85}}, 
     0.002415247101687503}}, {{{70.47570603430913, 51.86457391959739, 
      29.162903296745}, 29.68388136790197, 
     {1.110168089319956, 1.229103130012056, 1.605296816664047}, 
     {"R", "red", {1, 0, 0}}, 0.1056261712449114}, 
    {{-131.8203292392433, 110.5253109936243, -42.31441492755766}, 
     121.4068101850715, {0.92595478850094, 0.990606158429391, 
      1.036764880056181}, {"G", "green", {0, 1, 0}}, 0.05635215606863495}, 
    {{10.13901422726245, 145.5863830742936, 32.00422215326372}, 
     48.36929902392832, {0.99999997235607, 0.999999394430672, 
      0.999999363999513}, {"Y", "yellow", {1, 1, 0}}, 0.0941468914421895}, 
    {{-46.13065222533805, -95.1451579123497, 62.98942951278652}, 
     71.97467040860237, {1.001328371345833, 0.998392114075059, 
      1.000146909152531}, {"B", "blue", {0, 0, 1}}, 0.07133269965963059}, 
    {{32.75157702275883, 59.42631853985989, -12.55584806927745}, 
     46.02264175636888, {0.957123479231195, 0.927615418279127, 
      1.03830268256839}, {"Bn", "brown", {11/17, 14/85, 14/85}}, 
     0.0807578947093132}, {{155.4843723215934, -42.08424969329905, 
      23.53141572043431}, 64.96697323168849, 
     {0.999999999983169, 1.000000000000024, 0.999999999998758}, 
     {"Pl", "purple", {32/51, 32/255, 16/17}}, 0.1196564336401132}, 
    {{83.7712989081118, 1.040827997089359, 124.7172473269688}, 
     57.16828391984896, {0.984533159612614, 1.591777215063118, 
      0.924668144895979}, {"Pk", "pink", {1, 64/85, 203/255}}, 
     0.06077940640323634}, {{69.91455255177499, 102.4758032285154, 
      47.23362527276361}, 33.24243730153221, {1., 1., 1.}, 
     {"Or", "orange", {1, 11/17, 0}}, 0.106912407794282}}}
 
bct`Private`PointOutSamples[bct`Private`any_:False, num_:1, theta_:1/2, 
   bct`Private`categories_:
    {{{{0, 0, 10.11758111488888}, 8.59301680280366, 
       {1., 1., 0.999999999707514}, {"Bk", "black", {0, 0, 0}}, 
       0.00006706993659198796}, 
      {{0, 0, 99.1381861709482}, 8.33652823891848, 
       {1., 1., 0.999999999989715}, {"W", "white", {1, 1, 1}}, 
       0.1165768032502432}, {{0, 0, 55.41087739347514}, 21.3036871078195, 
       {1., 1., 1.000006840564639}, {"Gy", "gray", {0.85, 0.85, 0.85}}, 
       0.002415247101687503}}, 
     {{{70.47570603430913, 51.86457391959739, 29.162903296745}, 
       29.68388136790197, {1.110168089319956, 1.229103130012056, 
        1.605296816664047}, {"R", "red", {1, 0, 0}}, 0.1056261712449114}, 
      {{-131.8203292392433, 110.5253109936243, -42.31441492755766}, 
       121.4068101850715, {0.92595478850094, 0.990606158429391, 
        1.036764880056181}, {"G", "green", {0, 1, 0}}, 0.05635215606863495}\
       , {{10.13901422726245, 145.5863830742936, 32.00422215326372}, 
       48.36929902392832, {0.99999997235607, 0.999999394430672, 
        0.999999363999513}, {"Y", "yellow", {1, 1, 0}}, 0.0941468914421895}\
       , {{-46.13065222533805, -95.1451579123497, 62.98942951278652}, 
       71.97467040860237, {1.001328371345833, 0.998392114075059, 
        1.000146909152531}, {"B", "blue", {0, 0, 1}}, 0.07133269965963059}, 
      {{32.75157702275883, 59.42631853985989, -12.55584806927745}, 
       46.02264175636888, {0.957123479231195, 0.927615418279127, 
        1.03830268256839}, {"Bn", "brown", {11/17, 14/85, 14/85}}, 
       0.0807578947093132}, {{155.4843723215934, -42.08424969329905, 
        23.53141572043431}, 64.96697323168849, 
       {0.999999999983169, 1.000000000000024, 0.999999999998758}, 
       {"Pl", "purple", {32/51, 32/255, 16/17}}, 0.1196564336401132}, 
      {{83.7712989081118, 1.040827997089359, 124.7172473269688}, 
       57.16828391984896, {0.984533159612614, 1.591777215063118, 
        0.924668144895979}, {"Pk", "pink", {1, 64/85, 203/255}}, 
       0.06077940640323634}, {{69.91455255177499, 102.4758032285154, 
        47.23362527276361}, 33.24243730153221, {1., 1., 1.}, 
       {"Or", "orange", {1, 11/17, 0}}, 0.106912407794282}}}, 
   bct`Private`transform_:bct`Private`RgbToAbl, 
   bct`Private`filename_:"./.subsampled.ppm", 
   bct`Private`sizeFilename_:"./.subsampled.xy", 
   bct`Private`thetaFudge_:0.99] := 
  Module[{bct`Private`samples, dims, bct`Private`origDims, 
    bct`Private`mtime, bct`Private`mtime1, name, r, s, 
    bct`Private`results = {}}, Print["Please sample the image"]; 
    bct`Private`mtime = FileDate[bct`Private`filename]; 
    While[FileDate[bct`Private`filename] == bct`Private`mtime, Pause[0.5]]; 
    Pause[0.5]; Print["working..."]; 
    bct`Private`origDims = 
     bct`Private`GetOriginalDimensions[bct`Private`sizeFilename]; 
    bct`Private`samples = ReadPpmFile[bct`Private`filename]; 
    dims = Dimensions[bct`Private`samples]; 
    s = Flatten[bct`Private`samples, 1]; 
    bct`Private`samples = bct`Private`transform /@ s; 
    bct`Private`mtime = FileDate[bct`Private`filename]; 
    Print["Enter color names as quoted [list of] string[s], or \"\" to\
      stop"]; While[(name = Input["Color: "]) =!= "", 
     If[(bct`Private`mtime1 = FileDate[bct`Private`filename]) =!= 
        bct`Private`mtime, Print["Re-reading image file, hang on..."]; 
        Pause[0.5]; bct`Private`origDims = 
         bct`Private`GetOriginalDimensions[bct`Private`sizeFilename]; 
        bct`Private`samples = ReadPpmFile[bct`Private`filename]; 
        dims = Dimensions[bct`Private`samples]; 
        s = Flatten[bct`Private`samples, 1]; 
        bct`Private`samples = bct`Private`transform /@ s; 
        bct`Private`mtime = bct`Private`mtime1]; 
      If[bct`Private`any, If[ListQ[name], 
        r = AnyComplexExample[name, bct`Private`samples, 
          bct`Private`categories, num, theta, bct`Private`thetaFudge], 
        r = AnyExample[name, bct`Private`samples, bct`Private`categories, 
          num, theta, bct`Private`thetaFudge]], 
       If[ListQ[name], r = 
         BestComplexExample[name, bct`Private`samples, 
          bct`Private`categories, num, theta, bct`Private`thetaFudge], 
        r = BestExample[name, bct`Private`samples, bct`Private`categories, 
          num, theta, bct`Private`thetaFudge]]]; 
      bct`Private`WriteIndexFile[r, dims, bct`Private`origDims]; 
      bct`Private`results = 
       Append[bct`Private`results, 
        If[Length[Dimensions[r]] > 1, 
         ({s[[1 + #1[[3]]]], #1[[2]], #1[[3]]} & ) /@ r, 
         If[r =!= {}, {s[[1 + r[[3]]]], r[[2]], r[[3]]}, r]]]; Print[r]; Null
      ]; bct`Private`WriteIndexFile[{{{}, 0, -1}}, dims, 
     bct`Private`origDims]; bct`Private`results]
 
AnyComplexExample[complexName_, stimuli_, categories_, num_:1, theta_:1/2, 
   thetaFudge_:0.99] := 
  Module[{lim = 1, m1, m2, cats, match, stim}, 
   Which[Length[complexName] === 1, 
     Return[AnyExample[complexName[[1]], stimuli, categories, num, theta, 
       thetaFudge]], StringMatchQ[complexName[[1]], "somewhat", 
      IgnoreCase -> True], lim = thetaFudge*theta]; 
    cats = {First[Select[Flatten[categories, 1], 
        Apply[Or, (StringMatchQ[#1, complexName[[-1]], 
              IgnoreCase -> True] & ) /@ Take[#1[[4]], 2]] & , 1]], 
      First[Select[Flatten[categories, 1], 
        Apply[Or, (StringMatchQ[#1, complexName[[-2]], 
              IgnoreCase -> True] & ) /@ Take[#1[[4]], 2]] & , 1]]}; 
    stim = Transpose[{stimuli, Range[0, Length[stimuli] - 1]}]; 
    match = Select[stim, (m1 = 
           NGaussian[cats[[1,3]]*#1[[1]], cats[[1,3]]*cats[[1,1]], 
            cats[[1,2]]]) >= thetaFudge*theta && 
        (m2 = NGaussian[cats[[2,3]]*#1[[1]], cats[[2,3]]*cats[[2,1]], 
            cats[[2,2]]]) >= 0.75*thetaFudge*theta && m1 > m2 && m2 < lim & \
       , num]; match = 
     ({#1[[1]], {NGaussian[cats[[1,3]]*#1[[1]], cats[[1,3]]*cats[[1,1]], 
           cats[[1,2]]], NGaussian[cats[[2,3]]*#1[[1]], 
           cats[[2,3]]*cats[[2,1]], cats[[2,2]]]}, #1[[2]]} & ) /@ match; 
    Take[match, If[num > Length[match], Length[match], num]]]
 
AnyExample[name_, stimuli_, categories_, num_:1, theta_:1/2, 
   thetaFudge_:0.99] := 
  Module[{cat, match, stim}, cat = 
     First[Select[Flatten[categories, 1], 
       Apply[Or, (StringMatchQ[name, #1, IgnoreCase -> True] & ) /@ 
          Take[#1[[4]], 2]] & , 1]]; 
    stim = Transpose[{stimuli, Range[0, Length[stimuli] - 1]}]; 
    match = Select[stim, NGaussian[cat[[3]]*#1[[1]], cat[[3]]*cat[[1]], 
         cat[[2]]] >= thetaFudge*theta & , num]; 
    match = ({#1[[1]], NGaussian[cat[[3]]*#1[[1]], cat[[3]]*cat[[1]], 
          cat[[2]]], #1[[2]]} & ) /@ match; 
    Take[match, If[num > Length[match], Length[match], num]]]
 
BestComplexExample[complexName_, stimuli_, categories_, num_:1, theta_:1/2, 
   thetaFudge_:0.99] := 
  Module[{lim = 1, m1, m2, cats, match, stim}, 
   Which[Length[complexName] === 1, 
     Return[BestExample[complexName[[1]], stimuli, categories, num, theta, 
       thetaFudge]], StringMatchQ[complexName[[1]], "somewhat", 
      IgnoreCase -> True], lim = thetaFudge*theta]; 
    cats = {First[Select[Flatten[categories, 1], 
        Apply[Or, (StringMatchQ[#1, complexName[[-1]], 
              IgnoreCase -> True] & ) /@ Take[#1[[4]], 2]] & , 1]], 
      First[Select[Flatten[categories, 1], 
        Apply[Or, (StringMatchQ[#1, complexName[[-2]], 
              IgnoreCase -> True] & ) /@ Take[#1[[4]], 2]] & , 1]]}; 
    stim = Transpose[{stimuli, Range[0, Length[stimuli] - 1]}]; 
    match = Select[stim, (m1 = 
           NGaussian[cats[[1,3]]*#1[[1]], cats[[1,3]]*cats[[1,1]], 
            cats[[1,2]]]) >= thetaFudge*theta && 
        (m2 = NGaussian[cats[[2,3]]*#1[[1]], cats[[2,3]]*cats[[2,1]], 
            cats[[2,2]]]) >= 0.75*thetaFudge*theta && m1 > m2 && m2 < lim & ]\
     ; match = ({#1[[1]], {NGaussian[cats[[1,3]]*#1[[1]], 
           cats[[1,3]]*cats[[1,1]], cats[[1,2]]], 
          NGaussian[cats[[2,3]]*#1[[1]], cats[[2,3]]*cats[[2,1]], 
           cats[[2,2]]]}, #1[[2]]} & ) /@ match; 
    Take[Sort[match, Function[{m1, m2}, m1[[2,1]] > m2[[2,1]]]], 
     If[num > Length[match], Length[match], num]]]
 
BestExample[name_, stimuli_, categories_, num_:1, theta_:1/2, 
   thetaFudge_:0.99] := 
  Module[{cat, match, stim}, cat = 
     First[Select[Flatten[categories, 1], 
       Apply[Or, (StringMatchQ[name, #1, IgnoreCase -> True] & ) /@ 
          Take[#1[[4]], 2]] & , 1]]; 
    stim = Transpose[{stimuli, Range[0, Length[stimuli] - 1]}]; 
    match = Select[stim, NGaussian[cat[[3]]*#1[[1]], cat[[3]]*cat[[1]], 
         cat[[2]]] >= thetaFudge*theta & ]; 
    match = ({#1[[1]], NGaussian[cat[[3]]*#1[[1]], cat[[3]]*cat[[1]], 
          cat[[2]]], #1[[2]]} & ) /@ match; 
    Take[Sort[match, Function[{m1, m2}, m1[[2]] > m2[[2]]]], 
     If[num > Length[match], Length[match], num]]]
 
bct`Private`WriteIndexFile[bct`Private`sampleList_, dims_, 
   bct`Private`origDims_, bct`Private`filename_:"./.returnedpoints.xy"] :\
   = Module[{bct`Private`f}, bct`Private`f = OpenWrite[bct`Private`filename]; 
    If[bct`Private`sampleList[[1,3]] < 0, Close[bct`Private`f]; Return[]]; 
    If[Length[bct`Private`sampleList] == 0, 
     WriteString[bct`Private`f, "-1 -1\n"]; Close[bct`Private`f]; Return[]]; 
    (WriteString[bct`Private`f, 
        ToString[Mod[#1[[3]], dims[[2]]]*bct`Private`origDims[[3]] + 
          bct`Private`origDims[[5]]], " ", 
        ToString[Floor[#1[[3]]/dims[[2]]]*bct`Private`origDims[[4]] + 
          bct`Private`origDims[[6]]], "\n"] & ) /@ bct`Private`sampleList; 
    WriteString[bct`Private`f, "-1 -1\n"]; Close[bct`Private`f]; Null]
 
bct`Private`SelectObject[bct`Private`categories_:
    {{{{0, 0, 10.11758111488888}, 8.59301680280366, 
       {1., 1., 0.999999999707514}, {"Bk", "black", {0, 0, 0}}, 
       0.00006706993659198796}, 
      {{0, 0, 99.1381861709482}, 8.33652823891848, 
       {1., 1., 0.999999999989715}, {"W", "white", {1, 1, 1}}, 
       0.1165768032502432}, {{0, 0, 55.41087739347514}, 21.3036871078195, 
       {1., 1., 1.000006840564639}, {"Gy", "gray", {0.85, 0.85, 0.85}}, 
       0.002415247101687503}}, 
     {{{70.47570603430913, 51.86457391959739, 29.162903296745}, 
       29.68388136790197, {1.110168089319956, 1.229103130012056, 
        1.605296816664047}, {"R", "red", {1, 0, 0}}, 0.1056261712449114}, 
      {{-131.8203292392433, 110.5253109936243, -42.31441492755766}, 
       121.4068101850715, {0.92595478850094, 0.990606158429391, 
        1.036764880056181}, {"G", "green", {0, 1, 0}}, 0.05635215606863495}\
       , {{10.13901422726245, 145.5863830742936, 32.00422215326372}, 
       48.36929902392832, {0.99999997235607, 0.999999394430672, 
        0.999999363999513}, {"Y", "yellow", {1, 1, 0}}, 0.0941468914421895}\
       , {{-46.13065222533805, -95.1451579123497, 62.98942951278652}, 
       71.97467040860237, {1.001328371345833, 0.998392114075059, 
        1.000146909152531}, {"B", "blue", {0, 0, 1}}, 0.07133269965963059}, 
      {{32.75157702275883, 59.42631853985989, -12.55584806927745}, 
       46.02264175636888, {0.957123479231195, 0.927615418279127, 
        1.03830268256839}, {"Bn", "brown", {11/17, 14/85, 14/85}}, 
       0.0807578947093132}, {{155.4843723215934, -42.08424969329905, 
        23.53141572043431}, 64.96697323168849, 
       {0.999999999983169, 1.000000000000024, 0.999999999998758}, 
       {"Pl", "purple", {32/51, 32/255, 16/17}}, 0.1196564336401132}, 
      {{83.7712989081118, 1.040827997089359, 124.7172473269688}, 
       57.16828391984896, {0.984533159612614, 1.591777215063118, 
        0.924668144895979}, {"Pk", "pink", {1, 64/85, 203/255}}, 
       0.06077940640323634}, {{69.91455255177499, 102.4758032285154, 
        47.23362527276361}, 33.24243730153221, {1., 1., 1.}, 
       {"Or", "orange", {1, 11/17, 0}}, 0.106912407794282}}}, 
   bct`Private`transform_:bct`Private`RgbToAbl, 
   bct`Private`imageFilename_:"./.subsampled.ppm", 
   bct`Private`sampleFilename_:"./.sample.ppm"] := 
  Module[{bct`Private`samples = {}, bct`Private`smple, dims, 
    bct`Private`mtime, name, s = {}, r, bct`Private`results = {}}, 
   Print["object sample?"]; bct`Private`mtime = 
     FileDate[bct`Private`sampleFilename]; 
    While[FileDate[bct`Private`sampleFilename] == bct`Private`mtime, 
     Pause[0.25]]; Pause[0.25]; 
    While[(bct`Private`smple = ReadPpmFile[bct`Private`sampleFilename]) =!= 
      {}, bct`Private`mtime = FileDate[bct`Private`sampleFilename]; 
      bct`Private`samples = 
       Append[bct`Private`samples, 
        {bct`Private`transform[bct`Private`smple[[1,1]]], 
         bct`Private`GetPointCoords[]}]; 
      s = Append[s, bct`Private`smple[[1,1]]]; Print["object sample?"]; 
      While[FileDate[bct`Private`sampleFilename] == bct`Private`mtime, 
       Pause[0.25]]; Pause[0.25]]; 
    Print["Enter color names as quoted string[s], or \"\" to stop"]; 
    While[(name = Input["Color: "]) =!= "", 
     r = BestExample[name, First /@ bct`Private`samples, 
        bct`Private`categories, 1, 0.01]; 
      If[r =!= {}, bct`Private`PutPointCoords[bct`Private`samples[[1 + 
           r[[1,3]],2]]]; bct`Private`results = 
         Append[bct`Private`results, 
          {s[[1 + r[[1,3]]]], r[[1,2]], r[[1,3]]}]; Print[r], 
       bct`Private`PutPointCoords[{-1, -1}]; 
        bct`Private`results = Append[bct`Private`results, r]; Print["huh?"]]]\
     ; bct`Private`PutPointCoords[{-1, -1}]; {s, bct`Private`results}]
 
bct`Private`GetPointCoords[bct`Private`filename_:"./.sample.xy"] :=
  Module[{bct`Private`f, coords}, 
   bct`Private`f = OpenRead[bct`Private`filename]; 
    coords = Read[bct`Private`f, {Number, Number}]; Close[bct`Private`f]; 
    coords]
 
bct`Private`PutPointCoords[p_, 
   bct`Private`filename_:"./.returnedpoints.xy"] := 
  Module[{bct`Private`f}, bct`Private`f = OpenWrite[bct`Private`filename]; 
    WriteString[bct`Private`f, StringForm["`` ``\n", p[[1]], p[[2]]]]; 
    WriteString[bct`Private`f, "-1 -1\n"]; Close[bct`Private`f]; Null]
 
demo2[bct`Private`file_:"./utensils.ppm"] := 
  Module[{}, RunThrough[StringJoin["export LD_LIBRARY_PATH; ", "LD_LIBRARY_\
       PATH=/util/X11/lib:/usr/openwin/lib:/usr/lib:/util/lib; ", "cat ",
       bct`Private`file, " | ./ColorNaming &"], Null]; Pause[5]; 
    Print["*** No Adaptation, theta=0.35, RgbToAbl ***"]; 
    bct`Private`DefaultBW[]; Print["*** Color Naming, simple names ***"]; 
    bct`Private`NameSamples[False, 1, 0.35, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Color Naming, complex names ***"]; 
    bct`Private`NameSamples[True, 1, 0.35, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Pointing Out Colors, simple or\
      complex names ***"]; bct`Private`PointOutSamples[False, 3, 0.01, 
     bct`Private`BccEnglishAblCat6, bct`Private`RgbToAbl]; 
    Print["*** Choosing Objects by Color, simple names ***"]; 
    bct`Private`SelectObject[bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Null]
 
bct`Private`DefaultBW[] := 
  ({bct`Private`BlackRGB, bct`Private`WhiteRGB} = {{0, 0, 0}, {1, 1, 1}}; 
    Print[StringForm["{BlackRGB, WhiteRGB} = ``", 
      {bct`Private`BlackRGB, bct`Private`WhiteRGB}]])
 
demo3[bct`Private`file_:"./anthropologists_umax.ppm.gc"] := 
  Module[{}, RunThrough[StringJoin["export LD_LIBRARY_PATH; ", "LD_LIBRARY_\
       PATH=/util/X11/lib:/usr/openwin/lib:/usr/lib:/util/lib; ", "cat ",
       bct`Private`file, " | ./ColorNaming &"], Null]; Pause[5]; 
    Print["*** Adaptation, theta=0.30, RgbToAbl ***"]; bct`Private`FindBW[]; 
    Print["*** Color Naming, simple names ***"]; 
    bct`Private`NameSamples[False, 1, 0.3, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Color Naming, complex names ***"]; 
    bct`Private`NameSamples[True, 1, 0.3, bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Print["*** Pointing Out Colors, simple or\
      complex names ***"]; bct`Private`PointOutSamples[False, 3, 0.01, 
     bct`Private`BccEnglishAblCat6, bct`Private`RgbToAbl]; 
    Print["*** Choosing Objects by Color, simple names ***"]; 
    bct`Private`SelectObject[bct`Private`BccEnglishAblCat6, 
     bct`Private`RgbToAbl]; Null]
