6
\$\begingroup\$

Your challenge is to write a program that takes input from stdin, creates a set of 1 to ncolors and outputs them on stdout, formatted as a 6-digit-hex value with a prefixed #.

  • The starting color should have following values (in a hsv colorspace) (pesudocode)
    • h=rand(), s=0.5, v=0.95.
  • Every subsequent color should have its hue value increased by
    • 1/Φ
  • After every 5th color, the saturation and value for the forthcoming colors should be
    • saturation - increased by 0.1
    • value - decreased by 0.2

e.g.

Color  #    h         s       v  
       1  0.5        0.5     0.95
       2  0.118...   0.5     0.95
      11  0.680...   0.7     0.55

Input

Your program shall receive an Integer nas input (stdin), which defines the number of colors, to be generated. Where 0 < n < 16

Output

  • On each call, the hues' start value should be different from the last call (Don't just take the same random start number on every call)
  • Output should be on stdout with one color per line.
  • The array shall contain n different hex codes.
  • The hex codes should be prefixed with a "#" and be padded with a "0", such that you always get a 7 character string like "#FFFFFF"

Additional Rules

  • The use of built in functions/tools/etc for the following conversions is forbidden
    • HSV->RGB
    • RGB->HEX representation of RGBa function which specifically converts rgb to hex, generic tools like sprintf are ok.
    • HSV->HEX representation of RGBjust to make sure...

Scoring

The size of your code in bytes.

Test Cases(result of rand() in parantheses)

3 (0.35389856481924653) -> 
    #79f388
    #f3798e
    #79b1f3

8 (0.763850917108357) -> 
    #c079f3
    #79f39d
    #f37979
    #4c6ec0
    #90c04c
    #c04cb1
    #4cc0ac
    #c08b4c

15 (0.10794945224188268) -> 
    #f3c879
    #a479f3
    #79f381
    #f37995
    #79b8f3
    #aac04c
    #b44cc0
    #4cc092
    #c0704c
    #4f4cc0
    #448c2a
    #8c2a61
    #2a7e8c
    #8c7e2a
    #612a8c 

for a visualization of the codes, you can paste your output here

I will change the accepted Answer, as soon as new appear

\$\endgroup\$
  • \$\begingroup\$ Could you include the value received from rand() in the test cases to make testing easier? \$\endgroup\$ – flornquake Aug 21 '13 at 11:03
  • \$\begingroup\$ Can you define "rgb->hex tool"? Does sprintf("%2x"*3,r,g,b) count? \$\endgroup\$ – John Dvorak Aug 21 '13 at 11:47
  • \$\begingroup\$ @JanDvorak I think sprintf is definetly eligible \$\endgroup\$ – C5H8NNaO4 Aug 21 '13 at 12:07
  • \$\begingroup\$ @flornquake Yupp, i'll update the question \$\endgroup\$ – C5H8NNaO4 Aug 21 '13 at 12:07
  • 1
    \$\begingroup\$ @DavidCarraher Φ is the Golden Ratio, approximately 1.618. Hue is defined on [0, 1], the value one being exactly once around the unit circle. 1/Φ (or equivalently Φ-1) is approximately 3.883 radians, or 222.5 degrees. \$\endgroup\$ – primo Aug 22 '13 at 4:20
5
\$\begingroup\$

Python 184 bytes

from random import*
h,s,v=random()*6,.5,243.2
for i in range(input()):
 h+=3.708;print'#'+'%02x'*3%((v,v-v*s*abs(1-h%2),v-v*s)*3)[5**int(h)/3%3::int(h)%2+1][:3]
 if i%5/4:s+=.1;v-=51.2

The HSV ⇒ RGB conversion is taken directly from wikipedia.

A few implementation notes.

  • h is defined as its proper value times 6, which simplifies region determination.
  • v is defined as its proper value times 256, which propagates through to all three RGB values.
  • 6/Φ (approximately ~3.708) is used instead of 1/Φ, in agreement with the scaling of h. Similarly, the decrement value for v is also scaled by 256.
  • The bit of magic
    ((v,v-v*s*abs(1-h%2),v-v*s)*3)[5**int(h)/3%3::int(h)%2+1][:3]
    is logically equivalent to
    c=v*s;m=v-c;x=c-c*abs(1-h%2)+m;c+=m;[(c,x,m),(x,c,m),(m,c,x),(m,x,c),(x,m,c),(c,m,x)][int(h)%6]

Sample usage:

$ echo 3 | python color-set.py
#f37994
#79b8f3
#dbf379

$ echo 8 | python color-set.py
#d2f379
#f079f3
#79f3cc
#f3a979
#8579f3
#62c04c
#c04c84
#4ca6c0

$ echo 15 | python color-set.py
#7984f3
#a8f379
#f379cb
#79eff3
#f3d379
#804cc0
#4cc05e
#c04c5c
#4c7dc0
#9fc04c
#8b2a8c
#2a8c6f
#8c522a
#352a8c
#3b8c2a

A visualization of the values for n = 15: enter image description here
and the corresponding html.

\$\endgroup\$
  • \$\begingroup\$ Nice solution! And Thanks for the update about the HSV to RGB conversion and the visualization =) \$\endgroup\$ – C5H8NNaO4 Aug 23 '13 at 8:03
3
\$\begingroup\$

As I saw by other People, i start with my own solution

Javascript, 418433

a=prompt();x=0.6180339887;r=[];m=Math.random;p=[m(),0.5,0.95];function h(c,g,d){k=~~(6*c);b=6*c-k;c=d*(1-g);l=d*(1-b*g);g=d*(1-(1-b)*g);f=e=b=255;1>k?(b=d,e=g,f=c):2>k?(b=l,e=d,f=c):3>k?(b=c,e=d,f=g):4>k?(b=c,e=l,f=d):5>k?(b=g,e=c,f=d):(b=d,e=c,f=l);return"#"+[b,e,f].map(function(c){return 16>(c=0|256*c)?0:""+c.toString(16)}).join("")}for(;a--;)r.push(h.apply(h,p)),p[0]+=x,p[0]%=1,!(a%5)&&(p[1]+=0.1,p[2]-=0.2);console.log(r.join("\n"))

Damn, i did something wrong in the previous versioon when inlining the function, i shouldn't write the code golfed from beginning

Sample Output

enter image description here

#79f3a0
#f37d79
#7999f3
#bcf379
#f379e0
#4cc0b0
#c08e4c
#6d4cc0
#4ec04c
#c04c6f
#2a648c
#818c2a
#7b2a8c
#2a8c5e
#8c412a
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.