1

カラーピッカーを作成しています。「入力」と「出力」があります。入力には 4 つのスクロールバーがあり、出力にはプレビューがあり、出力には 3 つのテキスト ボックスがあります。

プレビューをクリックすると、ウィンドウが開き、カラー ダイアログが表示されます。16 進数で正常に動作し、16 進数値を返します。

しかし、スクロールバーに返される16進値を入れたいです。

RGBまたはIntに変換してから、スクロールバーの値を設定する必要があると思います。

どうやってやるの?

pictureBox からのコード:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pictureBox1.BackColor = colorDialog1.Color;
            string color, colorRGB;
            color = string.Format("{0:X8}", pictureBox1.BackColor.ToArgb());
            colorRGB = string.Format("{0:X6}", pictureBox1.BackColor.ToArgb());
            hexResult = color;
            pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " 0x" + hexResult;
            hexTextBox.Text = "0x" + hexResult;
        }
    }

スクロールバーからのコード:

    private void alphaScroll_Scroll(object sender, ScrollEventArgs e)
    {
        Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
        pictureBox1.BackColor = previewColor;

        int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
        hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));

        hexTextBox.Text = hexResult;
        pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
        alphaResultLabel.Text = alphaScroll.Value.ToString();
    }

    private void redScroll_Scroll(object sender, ScrollEventArgs e)
    {
        Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
        pictureBox1.BackColor = previewColor;

        int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
        hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));

        int red = redScroll.Value;
        int green = greenScroll.Value;
        int blue = blueScroll.Value;

        getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
        embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
        hexTextBox.Text = hexResult;
        pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
        redResultLabel.Text = redScroll.Value.ToString();
    }

    private void greenScroll_Scroll(object sender, ScrollEventArgs e)
    {
        Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
        pictureBox1.BackColor = previewColor;

        int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
        hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));

        int red = redScroll.Value;
        int green = greenScroll.Value;
        int blue = blueScroll.Value;

        getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
        embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
        hexTextBox.Text = hexResult;
        pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
        greenResultLabel.Text = greenScroll.Value.ToString();
    }

    private void blueScroll_Scroll(object sender, ScrollEventArgs e)
    {
        Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
        pictureBox1.BackColor = previewColor;

        int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
        hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));

        int red = redScroll.Value;
        int green = greenScroll.Value;
        int blue = blueScroll.Value;

        getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
        embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
        hexTextBox.Text = hexResult;
        pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
        blueResultLabel.Text = blueScroll.Value.ToString();
    }
4

2 に答える 2

0

免責事項:これは、問題のコードを置き換えて、作成者が質問に投稿したものです。質問をロールバックし、解決したコードをコミュニティ wiki の回答としてここに配置しました。


誰かが私と同じ質問を受けている場合、解決策は次のとおりです。

string c = color.ToString();
string redHex = c.Substring(0, 2);
string greenHex = c.Substring(2, 2);
string blueHex = c.Substring(4, 2);
string alphaHex = c.Substring(6, 2);

int red = Convert.ToInt32(redHex, 16);
int green = Convert.ToInt32(greenHex, 16);
int blue = Convert.ToInt32(blueHex, 16);
int alpha = Convert.ToInt32(alphaHex, 16);

alphaScroll.Value = alpha;
redScroll.Value = red;
greenScroll.Value = green;
blueScroll.Value = blue;

alphaResultLabel.Text = alphaScroll.Value.ToString();
redResultLabel.Text = redScroll.Value.ToString();
greenResultLabel.Text = greenScroll.Value.ToString();
blueResultLabel.Text = blueScroll.Value.ToString();
于 2014-05-08T20:32:20.637 に答える
0

.NET にはそのための関数があります。

Color c = System.Drawing.ColorTranslator.FromHtml(hmtlcolor);
于 2013-06-12T18:40:59.870 に答える