Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:42:45+00:00 2026-06-14T05:42:45+00:00

I need to create a caption for a picture using two colors, all the

  • 0

I need to create a caption for a picture using two colors, all the words are in black except one in other color…

I’ve been reading the docs of RMagick I can’t find a way of doing this… What I’m using right now to create the text is:

txt_name = Draw.new
image.annotate(txt_name, 0,0,0,0, "This RED has to be in red") do
    self.gravity = Magick::NorthGravity
    self.pointsize = 20
    self.fill = '#000000'
    txt_name.font_weight = 100
    self.font_family = 'Arial'
end

Any idea?? Or something to read so I can make this work??

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:42:46+00:00Added an answer on June 14, 2026 at 5:42 am

    Something like the following should work for you. (Please note that this is just to show you the process; I wouldn’t recommend putting code like this into production. This is begging to be put in its own class EDIT: see below):

    draw               = Magick::Draw.new
    

    Set all of the text attributes on the draw object:

    draw.pointsize     = 20
    draw.fill          = '#000000'
    draw.gravity       = Magick::NorthGravity
    draw.font_weight   = 100
    draw.font_family   = "Arial"
    draw.font_style    = Magick::NormalStyle
    

    Get your image object (this one is just a new blank image):

    image              = Magick::Image.new(300,200)
    

    Set up the strings and measure them with get_type_metrics:

    black_text         = "This "
    red_text           = "RED"
    remainder          = " has to be in red"
    black_text_metrics = draw.get_type_metrics(black_text)
    red_text_metrics   = draw.get_type_metrics(red_text)
    remainder_metrics  = draw.get_type_metrics(remainder)
    

    Annotate with the black text:

    draw.annotate(image, 
                  black_text_metrics.width, 
                  black_text_metrics.height,
                  10,10,black_text)
    

    Change the color to red and add the red text:

    draw.fill = "#ff0000"
    draw.annotate(image,
                  red_text_metrics.width,
                  red_text_metrics.height,
                  10 + black_text_metrics.width, # x value set to the initial offset plus the width of the black text
                  10, red_text)
    

    Change the color back to black and add the remainder of the text:

    draw.fill = "#000000"
    draw.annotate(image,
                  remainder_metrics.width,
                  remainder_metrics.height,
                  10 + black_text_metrics.width + red_text_metrics.width,
                  10, remainder)
    

    Edit: This may give you an idea of how you could structure this a bit nicer:

    TextFragment = Struct.new(:string, :color)
    
    class Annotator
      attr_reader :text_fragments
      attr_accessor :current_color
    
      def initialize(color = nil)
        @current_color  = color || "#000000"
        @text_fragments = []
      end
    
      def add_string(string, color = nil)
        text_fragments << TextFragment.new(string, color || current_color)
      end
    end
    
    
    class Magick::Draw
      def annotate_multiple(image, annotator, x, y)
        annotator.text_fragments.each do |fragment|
          metrics   = get_type_metrics(fragment.string)
          self.fill = fragment.color
          annotate(image, metrics.width, metrics.height, x, y, fragment.string)
          x += metrics.width
        end
      end
    end
    

    and a usage example:

    image            = Magick::Image.new(300,200)
    draw             = Magic::Draw.new
    draw.pointsize   = 24
    draw.font_family = "Arial"
    draw.gravity     = Magick::NorthGravity
    annotator        = Annotator.new #using the default color (black)
    
    annotator.add_string "Hello "
    annotator.add_string "World", "#ff0000"
    annotator.add_string "!"
    
    draw.annotate_multiple(image, annotator, 10, 10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to create a custom DNS name server using C which will check against
I need to create a class with two properties: LogOutput ExceptionOutput These properties (Actions<>)
I need to create a caption bar button for a CDockablePane which will call
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I use MS SQL 2008 R2, I need create a Table with a CHECK
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.