private static Color DotColor(int value, int length) { float angle = 360f * value / length; ColorHSB color = new ColorHSB(angle, 0.75f, 0.75f); return(color.ToColor()); }
static public Color GetBodyColor(int worldIndex, bool isBouncy, bool mayBounce, bool doRechargePlayer) { Color color = Colors.GroundBaseColor(worldIndex); if (isBouncy) // Bouncy? Brighten it much! { ColorHSB colorHSB = new ColorHSB(color); colorHSB.s = Mathf.Min(1, 0.3f + colorHSB.s * 1.4f); colorHSB.b = Mathf.Min(1f, 0.3f + colorHSB.b * 1.4f); color = colorHSB.ToColor(); } if (!mayBounce) { color = Color.Lerp(color, Color.black, 0.7f); } if (!doRechargePlayer) { color = Color.Lerp(color, Color.black, 0.4f); } return(color); }
async private void Generate(object sender = null, EventArgs e = null) { if (isScrolling && sender != ScrollButton) { return; } do { // Create noise generators var genNoise = new FastNoiseLite(); var warpNoise = new FastNoiseLite(); int w = (int)PreviewWidth.Value; int h = (int)PreviewHeight.Value; if (w <= 0 || h <= 0) { return; } genNoise.SetNoiseType((FastNoiseLite.NoiseType)NoiseType.SelectedIndex); genNoise.SetRotationType3D((FastNoiseLite.RotationType3D)RotationType3D.SelectedIndex); genNoise.SetSeed((int)Seed.Value); genNoise.SetFrequency((float)Frequency.Value); genNoise.SetFractalType((FastNoiseLite.FractalType)FractalType.SelectedIndex); genNoise.SetFractalOctaves((int)FractalOctaves.Value); genNoise.SetFractalLacunarity((float)FractalLacunarity.Value); genNoise.SetFractalGain((float)FractalGain.Value); genNoise.SetFractalWeightedStrength((float)FractalWeightedStrength.Value); genNoise.SetFractalPingPongStrength((float)FractalPingPongStrength.Value); genNoise.SetCellularDistanceFunction((FastNoiseLite.CellularDistanceFunction)CellularDistanceFunction.SelectedIndex); genNoise.SetCellularReturnType((FastNoiseLite.CellularReturnType)CellularReturnType.SelectedIndex); genNoise.SetCellularJitter((float)CellularJitter.Value); warpNoise.SetSeed((int)Seed.Value); warpNoise.SetDomainWarpType((FastNoiseLite.DomainWarpType)DomainWarp.SelectedIndex - 1); warpNoise.SetRotationType3D((FastNoiseLite.RotationType3D)DomainWarpRotationType3D.SelectedIndex); warpNoise.SetDomainWarpAmp((float)DomainWarpAmplitude.Value); warpNoise.SetFrequency((float)DomainWarpFrequency.Value); warpNoise.SetFractalType((FastNoiseLite.FractalType)Enum.Parse(typeof(FastNoiseLite.FractalType), DomainWarpFractal.SelectedKey)); warpNoise.SetFractalOctaves((int)DomainWarpFractalOctaves.Value); warpNoise.SetFractalLacunarity((float)DomainWarpFractalLacunarity.Value); warpNoise.SetFractalGain((float)DomainWarpFractalGain.Value); if (ImageData.Length != w * h) { ImageData = new int[w * h]; } float noise; float minN = float.MaxValue; float maxN = float.MinValue; float avg = 0; bool get3d = Is3D.Checked == true; // Stupid! bool invert = Invert.Checked == true; // Timer Stopwatch sw = new Stopwatch(); int index = 0; if (VisualiseDomainWarp.Checked != true) { var noiseValues = new float[w * h]; bool warp = DomainWarp.SelectedIndex > 0; sw.Start(); for (var y = h / -2; y < h / 2; y++) { for (var x = w / -2; x < w / 2; x++) { FNfloat xf = x; FNfloat yf = y; FNfloat zf = zPos; if (get3d) { if (warp) { warpNoise.DomainWarp(ref xf, ref yf, ref zf); } noise = genNoise.GetNoise(xf, yf, zf); } else { if (warp) { warpNoise.DomainWarp(ref xf, ref yf); } noise = genNoise.GetNoise(xf, yf); } avg += noise; maxN = Math.Max(maxN, noise); minN = Math.Min(minN, noise); noiseValues[index++] = noise; } } sw.Stop(); avg /= index - 1; float scale = 255 / (maxN - minN); for (var i = 0; i < noiseValues.Length; i++) { int value = (int)MathF.Round(Math.Clamp((noiseValues[i] - minN) * scale, 0, 255)); if (invert) { value = 255 - value; } ImageData[i] = value; ImageData[i] |= value << 8; ImageData[i] |= value << 16; } } else { var noiseValues = new float[w * h * 3]; sw.Start(); for (var y = -h / 2; y < h / 2; y++) { for (var x = -w / 2; x < w / 2; x++) { FNfloat xf = x; FNfloat yf = y; FNfloat zf = zPos; if (get3d) { warpNoise.DomainWarp(ref xf, ref yf, ref zf); } else { warpNoise.DomainWarp(ref xf, ref yf); } xf -= x; yf -= y; zf -= zPos; avg += (float)(xf + yf); maxN = Math.Max(maxN, (float)Math.Max(xf, yf)); minN = Math.Min(minN, (float)Math.Min(xf, yf)); noiseValues[index++] = (float)xf; noiseValues[index++] = (float)yf; if (get3d) { avg += (float)zf; maxN = Math.Max(maxN, (float)zf); minN = Math.Min(minN, (float)zf); noiseValues[index++] = (float)zf; } } } sw.Stop(); if (get3d) { avg /= (index - 1) * 3; } else { avg /= (index - 1) * 2; } index = 0; float scale = 1 / (maxN - minN); for (var i = 0; i < ImageData.Length; i++) { Color color = new Color(); if (get3d) { color.R = (noiseValues[index++] - minN) * scale; color.G = (noiseValues[index++] - minN) * scale; color.B = (noiseValues[index++] - minN) * scale; } else { var vx = (noiseValues[index++] - minN) / (maxN - minN) - 0.5f; var vy = (noiseValues[index++] - minN) / (maxN - minN) - 0.5f; ColorHSB hsb = new ColorHSB(); hsb.H = MathF.Atan2(vy, vx) * (180 / MathF.PI) + 180; hsb.B = Math.Min(1.0f, MathF.Sqrt(vx * vx + vy * vy) * 2); hsb.S = 0.9f; color = hsb.ToColor(); } if (Invert.Checked == true) { color.Invert(); } ImageData[i] = color.ToArgb(); } } // Set image Bitmap = new Bitmap(w, h, PixelFormat.Format32bppRgb, ImageData); Image.Image = Bitmap; // Set info labels Time.Text = "Time (ms): " + sw.ElapsedMilliseconds.ToString(); Mean.Text = "Mean: " + avg.ToString(); Min.Text = "Min: " + minN.ToString(); Max.Text = "Max: " + maxN.ToString(); // Sets the client (inner) size of the window for your content ClientSize = new Size(Math.Max((int)PreviewWidth.Value, 768), Math.Max((int)PreviewHeight.Value, 768)) + windowSizeOffset; if (isScrolling) { await Task.Delay(50); try { zPos += ((float)Frequency.Value) * 100.0f; } catch (Exception ex) { MessageBox.Show("Frequency error: " + ex.ToString()); } } }while (isScrolling); }