きつくないcolor_id一覧を表示する(改善)
function generateSafeColorIds(num)
↑この関数にした
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
// id : 0 - 16777215 (24bit) // 1 -> [ 0, 0, 1 ] // 16777214 -> [ 255, 255, 254 ] // 16777215 -> [ 255, 255, 255 ] // 16777216 -> [ 0, 0, 0 ] function idToArray(id){ var n = id; var c = Math.floor(n % 256); n /= 256; var b = Math.floor(n % 256); n /= 256; var a = Math.floor(n % 256); n /= 256; return [a, b, c]; } function max(a){ var m = a[0]; for(var v of a){ if(m < v){ m = v; } } return m; } function min(a){ var m = a[0]; for(var v of a){ if(v < m){ m = v; } } return m; } // id : 0 - 16777215 (24bit) function isNgId(id){ var rgb = idToArray(id); // (python) if rgb[0] + rgb[1] + rgb[2] < 60 or rgb[0] + rgb[1] + rgb[2] > 700 or max(rgb) - min(rgb) > 200: if( (rgb[0] + rgb[1] + rgb[2]) < 60 || (rgb[0] + rgb[1] + rgb[2]) > 700 || max(rgb) - min(rgb) > 200 ){ return true; } else { return false; } } function p(a){ console.log(a); } function getRandomInt(min, max){ var d = Math.random(); var randomOffset = Math.floor(d * (max - min + 1)); return min + randomOffset; } function dise(){ let min = 1; let max = 6; return getRandomInt(min, max); } function dispArrayList(a){ for(let k in a){ let v = a[k]; p(k + " => " + v); } } function getRandomColorId(){ let start = 0; let end = 256*256*256 - 1; return getRandomInt(start, end); } function generateSafeColorIds(num){ let a = []; let cnt = 0; while(true){ if(num <= cnt){ break; } let n = getRandomColorId(); if(isNgId(n)){ continue; } a[cnt++] = n; } return a; } function main(){ let num = 50; let a = generateSafeColorIds(num); dispArrayList(a); } main(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
0 => 3350871 1 => 14057179 2 => 6081943 3 => 9022241 4 => 11033959 5 => 14322453 6 => 4443513 7 => 953484 8 => 6577271 9 => 7962921 10 => 4353179 11 => 12130079 12 => 14891355 13 => 4994683 14 => 4439075 15 => 3816539 16 => 11990858 17 => 9015204 18 => 478238 19 => 6677988 20 => 13206756 21 => 12192793 22 => 16266104 23 => 2965779 24 => 14067437 25 => 7990397 26 => 14268085 27 => 7751084 28 => 5633118 29 => 9726419 30 => 2037180 31 => 15175575 32 => 4342854 33 => 7228747 34 => 5738363 35 => 3074666 36 => 14631792 37 => 1489450 38 => 4220331 39 => 8751492 40 => 15542180 41 => 13281832 42 => 1418266 43 => 5961455 44 => 4861407 45 => 4925637 46 => 9193876 47 => 551716 48 => 431461 49 => 4810374 |